Thein's Logo

Git Rebase Vs Merge

Sep 09, 20191 min read
banner post image

"Should I rebase or merge master? What are the differences?" Not long ago, I remembered asking those questions during my first software internship. Let me share you some of my thoughts.

Main Goal

You are working in your dedicated feature-a branch. You want to update your branch with the latest master which is the main goal. Both git rebase and git merge can help you to achieve it. But they do it in different ways.

Branch with latest master

Git Rebase

git rebase shifts your entire feature branch and points to the latest master.

git checkout feature-a
git rebase master

Rebase master

Under the hood, git rebase rewrites project history. It creates brand new commits from the original feature branch_(see the diagram below)_. Rebasing gives us cleaner project history. However, git rebase can be a bit chaotic if someone is also working on your feature branch. In this case, I prefer to use git merge option.

Rebase under the hood Git rebase under the hood

Git Merge

git merge creates merge commit in your feature branch. It is much simpler than git rebase.

git checkout feature-a
git merge master

Merge master

Conclusion

To conclude, the followings are what I normally use in my day-to-day-work:

  • Use git rebase if I am the only one working on my feature branch.
  • Use git merge if other developer are also working on feature branch.
  • Only use either git rebase or git merge, one at a time. In other word,
  • Do not use git rebase if your branch has git merge master in the past (I messed this one up before.

More readings