Git Rebase Vs Merge
"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.
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.
git rebase shifts your entire feature branch and points to the latest master.
git checkout feature-a git 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.
git merge creates merge commit in your feature branch. It is much simpler than
git checkout feature-a git merge master
To conclude, the followings are what I normally use in my day-to-day-work:
git rebaseif I am the only one working on my feature branch.
git mergeif other developer are also working on feature branch.
- Only use either
git merge, one at a time. In other word,
- Do not use
git rebaseif your branch has
git merge masterin the past (I messed this one up before.