DEV Community

Cover image for Git Rebase vs Merge
Dhanushka madushan
Dhanushka madushan

Posted on

Git Rebase vs Merge

When you are using git, most probably you may used git merge or git rebase to combine two branches. Pretty much both of these commands do the same thing. But people select either of technique based on their requirements. This post is to compare the difference between git merge vs rebase and when to use.

Here below a diagram that visualize master branch to maintain the main code base and feature branch to develop some feature. You can see that feature branch was create from the master branch. While changes happening in feature branch, master branch also changing with new commits.
branch

Once you completed the feature implementation on feature branch, you need to combine it with the latest master branch. Here, you have two options. Either you can merge or rebase feature branch with the master.

Git Merge

If you merge two branches, it will create a new commit and combine master branch into the feature branch. Here, feature branch commits keep as it is and commit history does not change. We can represent above digram for git merge command as follows.

git merge

You can merge master branch into the feature branch by running following command

git checkout feature
git merge master

or with single line

git merge feature master

Git Rebase

Rebase is little different than merge. Rebase apply all feature branch changes on top of master branch by creating new commit for each of its previous commit messages. Which means that rebase command will change your commit history and regenerate commits again on top of master branch. Final output of git rebase can be represent as follows.

Rebase

Following command can be used to rebase feature branch on top of master branch. "-i" option used for interactive rebase. Otherwise you can simply use "git rebase master".

git checkout feature
git rebase -i master

Rebase or Merge

You can use either rebase or merge to combine two branches. Peoples are tend to use rebase over merge due to following facts.

  • There is no additional commit added to the merged branches
  • Rebase commit history is cleaner than merge history since rebase history does not have complex branches
  • Due to the branch complexity in merged git tree, some code changes are invisible. But rebase changes come from a specific and entitled commit.
  • Rebase make easier to use git log, git bisect, and gitk commands.

When to not to use Rebase

As we already discussed, rebase change the commit history. Therefore, it should not be apply on the public branches(eg: master) where other people are working on. This make git confuse that your master branch diverge from others master branch.

Conclusion

Both merge and rebase can be used to combine two branches. Merge command just unify your work with a commit without changing history. While rebase apply feature branch changes on top of master branch and change the history. If you prefer to have clean history, then you can use rebase. If you need to preserve the history changes, then merge would be the best choice.

Hope, this article helps you to learn the difference of git merge and git rebase. Comment down what is your preference and the reason. See you with another article. Cheers :).

References

https://www.atlassian.com/git/tutorials/merging-vs-rebasing

Top comments (1)

Collapse
 
kaydacode profile image
Kim Arnett 

Thank you for this! I was seeing some strange behaviors, all because I was using rebase and a peer was using merge. I couldn't figure out why it pretended I had never rebased before every time I did, wooo. Rewriting the commit history is the key. Thank you again, described a complicated tool very simply.