Comparison between "Merged" and "Rebase"

Comparison between "Merged" and "Rebase"
0
#1

Greetings, can anyone guide me on the advantages and disadvantages of a “merge” and a “rebase”? And what kind of projects are recommended one or the other. :thinking:

0 Likes

#2

So merge:

You have a main branch, call it master .
You have branched off to do some work, call that branch feature.

Stuff keeps gettting put into master while you’re working on feature, and you want to keep feature up-to-date. You can do that by git merge feature master, which will merge in all the changes made.

This creates a new commit on the feature branch, where all the changes get incorporated. It’s nice, because it’s non-destructive: both those branches are still there. And if there are problems, you can go back to before that merge commit really easily.

Rebase:

You have a main branch, call it master .
You have branched off to do some work, call that branch feature.

Stuff keeps gettting put into master while you’re working on feature, and you want to keep feature up-to-date. You can do that by git rebaseing against master, which will merge in all the changes made.

HOWEVER what rebase does is it [destructively] takes all the commits made on master that happened while you were working on feature and adds them onto your branch. The result of a rebase is one branch, which has a single line of commits that go

master up until point you branched off
        |
your feature commits
        |
commits that happened on master while you were working on feature

You don’t have those extra merge commits, and the git history becomes a pretty clean record of the changes made, in a linear order.

The severe disadvantage is that you can completely wreck the git history with rebase if you aren’t careful, and it is brutally hard to unpick the issues if you do mess it up. This is particularly problematic if you use it on public branches, when you start to need to force push to overwrite public branches that you’ve used rebase on.

So merge is IME what is normally used when working with a team. Rebase (again, IME) is something you would try to use exclusively locally, on your machine: you have your feature, then maybe you branch off and do some work, then you switch back, maybe there is a divergance from master that needs to be integrated, then you go back to the main feature etc. Instead of lots of merge commits, you can use rebase to keep a clean, linear git history of your feature, local on you machine, then when you do push your feature up.

2 Likes

#3

Wow, thanks! A truly enlightening answer.

0 Likes