From time to time I stumble upon a statement that 'git rebase' leads to a fewer number of conflicts than 'git merge', for example here: Why does git rebase often have fewer merge conflicts than a merge?. Does it mean that there may be situations where git-merge <BRANCH_A> <BRANCH_B>
would fail, I would do git reset --hard ORIG_HEAD
, then do git-rebase <BRANCH_B> <BRANCH_A>
and it would succeed? Can you give me an example of such situation?
Rebasing is better to streamline a complex history, you are able to change the commit history by interactive rebase. You can remove undesired commits, squash two or more commits into one or edit the commit message. Rebase will present conflicts one commit at a time whereas merge will present them all at once.
Conflicts can also happen during a rebase or an interactive rebase, when you're cherry picking in Git (i.e. when you choose a commit from one branch and apply it to another), when you're running git pull or even when reapplying a stash.
Git Merge Vs Git Rebase:Git merge is a command that allows you to merge branches from Git. Git rebase is a command that allows developers to integrate changes from one branch to another. In Git Merge logs will be showing the complete history of the merging of commits.
Rebasing is not going to magically remove all merge conflicts. In fact, you may encounter conflicts while rebasing. Sometimes, you will have to repeatedly resolve the same conflict while rebasing. However, merge conflicts happen because multiple changes happen to the same chunk of code simultaneously.
One advantage of rebasing is that you can rebase on a portion of the total commits, stabilize, commit resolutions and you now have an atomic unit of progress that can be resumed later. This can be MUCH less daunting. In fact you can rebase some then merge the rest. More flexible.
Imagine a history like this:
A-B-C-D-E “top-branch”
\
F-G-H
Now merging E
into H
means: “take a diff of B‣H
and a diff of B‣E
and find out how to combine them”.
Rebasing on the other hand means:
B‣F
and a diff of B‣E
and find out how to combine them into F’
F‣G
and a diff of F‣F’
and find out how to combine them into G’
G‣H
and a diff of G‣G’
and find out how to combine them into H’
So rebasing is exactly the same as merging F
, then G
and then H
into top-branch
.
The rebase method will do the merge in way more steps. This has advantages and disadvantages.
H
is actually a revert of F
.In my opinion rebase is actually the worse process and while that might be possible I am having trouble imagining a situation where it actually avoids conflicts. What seems quite likely is that you have a conflict that is only caused by a one-line change in F
. When that codeblock is further changed in G
, you might have to resolve all of these changes as one conflict in a merge, but you only have to resolve that one-line change in a rebase. But that is not really a difference, as the conflict will only look bigger, but should be just as easy to resolve.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With