Git Merge vs. Rebase
Git merge and rebase serve the same purpose - they combine multiple branches into one. Which should you use?
Join the DZone community and get the full member experience.Join For Free
Git merge and rebase serve the same purpose - they combine multiple branches into one. Although the final goal is the same, those two methods achieve it in different ways. Which method to use?
What Does Merge or Rebase Mean?
Here we have a sample repository that has two diverging branches: the master and the feature. We want to blend them together. Let's take a look how these methods can solve the problem.
When you run
git merge, your HEAD branch will generate a new commit, preserving the ancestry of each commit history.
Fast forward merge is a type of merge that doesn't create a commit, instead, it updates the branch pointer to the last commit.
The rebase re-writes the changes of one branch onto another without creating a new commit.
For every commit that you have on the feature branch and not in the master, a new commit will be created on top of the master. It will appear as if those commits were written on top of master branch all along.
Merging Pros and Cons
- Simple to use and understand.
- Maintains the original context of the source branch.
- The commits on the source branch are separated from other branch commits. This can be useful if you want to take the feature and merge it into another branch later.
- Preserves your commit history and keeps the history graph semantically correct.
Rebasing Pros and Cons
- Code history is simplified, linear and readable.
- Manipulating a single commit history is easier than a history of many separate feature branches with its additional commits.
- Clean, clear commit messages make it better to track a bug or when a feature was introduced. Avoid polluting the history with 20+ single-line commits!
- Lowering the feature down to a handful of commits can hide context.
- Rebasing doesn't work with pull requests, because you can't see what minor changes someone made. Rewriting of history is bad for teamwork!
You need to be more careful with rebasing than when merging.
Which Method to Choose?
When your team uses a feature based workflow or is not familiar with rebase, then
git merge is the right choice for you:
- It allows you to preserve the commit history for any given feature while not worrying about overriding commits and changing history. It helps you avoid unnecessary git reverts or resets!
- Different features remain isolated and don't interfere with existing commit histories.
- Can help you re-integrate a completed feature branch.
On the other hand, if you value more a clean, linear history then
git rebase may be most appropriate. You will avoid unnecessary commits and keep changes more centralized and linear!
If you rebase incorrectly and unintendedly rewrite the history, it can lead to serious issues, so make sure you know what you are doing!
Published at DZone with permission of Kristina Garcia Francisco, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.