Git: When to Rebase and when to Merge?
In Git, Merge and Rebase are two of the main ways to integrate changes from one branch to another branch. In this article, we’ll be looking at some of the best practices of using these two commands.
What is Merge?
Merges takes all the changes in one branch and merges them into another branch in one commit and maintains a commit history. It is very much like taking two threads and tying them up in a knot.
Let’s say you have created a branch from X for the purpose of adding a single feature. When you want to bring those changes back to X, you probably want a merge.
+ --- (C1) --- (C2) --- (C3) --- + / \ (c0) --- (c1) --- (c2) --- (c3) --- (c4) ----------------- (c4 + C3) [Merged]
- Simple to use and understand.
- The commits on the source branch remain separate from other branch commits. This separation can be useful in the case of feature branches, where you might want to take a feature and merge it into another branch later.
- Existing commits on the source branch are unchanged and remain valid. It doesn’t matter if they have been shared with others.
- History can become polluted by lots of merge commits.
- Visual charts of your repository can have non-informative branch lines.
What is Rebase?
Rebase changes the base of one branch on top of another branch. That means, it simply places all your commits on top of base commits. In this process, it rewrites the commit history.
Let’s suppose you started adding some features and then another developer made some unrelated changes. You probably want to pull and then rebase to base your changes from the current version of the repository.
Before Rebase :
+ --- (C1) --- (C2) --- (C3) / (c0) --- (c1) --- (c2) --- (c3) --- (c4) --- (c5)
(c0) --- (c1) --- (c2) --- (c3) --- (c4) --- (c5) --- (C1) --- (C2) --- (C3)
- Simplifies your history and visual charts.
- In Git, you may push commits you may want to rebase later (as a backup) but only if it’s to a remote branch that only you use. If anyone else checks out that branch and you later rebase it, it’s going to get very confusing.
- Each commit is rebased in order, and a conflict will interrupt the process of rebasing multiple commits. With a conflict, you have to resolve the conflict in order to continue the rebase.
Which one should I use ?
Following factors should be considered when choosing which operation to use :
- If you are planning to re-integrate a completed feature branch, use merge.
- If you have already pushed the branch you are working on, then you should not rebase but merge instead. For branches that have not been pushed, rebase, test and merge.
- If you are working on a shared branch or an open source project along with many other developers outside of your team, don’t rebase. Since rebase destroys the branch and other developers will have inconsistent repositories.
- If you think there is a chance you will want to revert some changes then use merge, since reverting a rebase is considerably difficult as compared to reverting a merge.
About CauseCode: We are a technology company specializing in Healthtech related Web and Mobile application development. We collaborate with passionate companies looking to change health and wellness tech for good. If you are a startup, enterprise or generally interested in digital health, we would love to hear from you! Let's connect at email@example.com