Merge or rebase: choosing
Compare merge and rebase on the same branch, see the two history shapes, and learn the one rule that decides which to use: has anyone else got these commits?
Merge or rebase: choosing
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Merge preserves history as it happened, with a merge commit joining two lines. Rebase rewrites your branch into one straight line.
- Both give the same final files. They differ in what the history looks like and whether commits get new IDs.
- Rebase commits nobody else has, and merge anything that is already shared.
- Rebasing a branch others have pulled makes their copy disagree with yours, and the repair is awkward.
- A common team default is to rebase your own feature branch onto main to update it, then merge it into main.
Same goal, two shapes
You have a feature branch and main has moved. You want your work on top of the latest main. Both commands get you there, and they leave different histories. Choosing is not a matter of taste alone, because one of them has a hazard.
I built the same situation twice, in two copies of tinynotes. feature has two commits (Add tags.py stub, Start tags). Since it branched off, main gained two commits (Add ABOUT, Add ABOUT2).
Option one: merge
Update the feature branch by merging main into it, then merge the feature into main:
$ git switch feature
$ git merge main -m "Merge main into feature"
Merge made by the 'ort' strategy.
ABOUT.txt | 1 +
ABOUT2.txt | 1 +
2 files changed, 2 insertions(+)
$ git log --oneline --graph -8
* ed3de8c Merge main into feature
|\
| * 31bb9ce Add ABOUT2
| * 77f9b20 Add ABOUT
* | 0df3a13 Start tags
* | 02dc96d Add tags.py stub
|/
* 3702e9b Merge branch 'fix-count'
The two lines run side by side and join. Every commit keeps its original ID (0df3a13 and 02dc96d are untouched), and the graph is an honest record: this work happened in parallel, and here is where it was combined. The cost is noise. Each time you pull main into a long-lived branch you add a “Merge main into feature” commit that carries no meaning of its own.
Option two: rebase
From the same starting point, rebase instead:
$ git switch feature
$ git rebase main
Rebasing (1/2)Rebasing (2/2)Successfully rebased and updated refs/heads/feature.
$ git log --oneline --graph -8
* ef114c1 Start tags
* c7d7118 Add tags.py stub
* 31bb9ce Add ABOUT2
* 77f9b20 Add ABOUT
* 3702e9b Merge branch 'fix-count'
A straight line, and the feature merges into main by fast-forward with no extra commit. It reads as though you wrote the feature after ABOUT2 landed. The cost is honesty: 0df3a13 and 02dc96d became ef114c1 and c7d7118. The history no longer shows that the work was done in parallel, and your original commits are gone from the branch.
Check the claim that the results are the same. In both copies, main ended up with a tree object of aec71b7..., the identical ID (git rev-parse HEAD^{tree}), which means byte-for-byte identical files. What differs is only the commit graph.
The rule that decides it
The hazard with rebase is the one from the last lesson: it creates new commits. If those commits exist only on your machine, nobody can be confused by the old ones. If somebody else already has the old ones, you’ve changed the past underneath them.
I tested this with a scratch bare repository standing in for a shared server. (Remote repositories and git push get their own group next, so treat the commands here as a preview.) A second clone, “Bob”, pulled feature and committed on top of it. Meanwhile I rebased feature onto main and force-pushed it (a force push replaces the remote branch, also covered in the remotes group). Then Bob ran a plain git pull:
$ git pull
From ~/m18/shared
+ 0df3a13...ef114c1 feature -> origin/feature (forced update)
hint: You have divergent branches and need to specify how to reconcile them.
...
fatal: Need to specify how to reconcile divergent branches.
The + and “(forced update)” are Git saying the remote branch was rewritten. Bob’s feature and the remote’s feature now share no recent commits, even though they contain “the same” work. Bob chose to merge, with git pull --no-rebase --no-edit:
Auto-merging tags.py
CONFLICT (add/add): Merge conflict in tags.py
Automatic merge failed; fix conflicts and then commit the result.
A conflict in a file neither of us had edited at the same place, because Git sees two different histories that both create tags.py. Bob now has to untangle it by hand, and so does every other person with a copy of the old branch. This is the real cost of rebasing shared commits, and it’s why the rule is:
If anyone else might have these commits, don’t rebase them. Merge instead.
Everything else is preference.
What teams actually do
The common patterns:
- Rebase your private feature branch onto
mainto keep it current while you work. Only you have those commits, so the new IDs hurt nobody. - Merge into
mainwhen the feature is done, often with--no-ffso the feature’s commits stay grouped under one merge commit. Or fast-forward after the rebase if the team prefers a linear history. - Never rebase
mainor any branch others build on.
GitHub’s pull request buttons offer merge, squash and rebase variants of this decision, and the collaboration group compares them. The fundamentals are the same as above.
Git can also be told which to prefer on pull, using pull.rebase. I hit exactly that prompt above. Set it once, for example git config --global pull.rebase false or true, so you aren’t asked each time.
What the video covers
ByteByteGo’s “Git MERGE vs REBASE: Everything You Need to Know” is under five minutes and was published around 2023. I had no transcript. From its title and description it compares the two commands with diagrams, and it is likely to come down on the same rule about shared branches. It is in English, with some promotion of the channel’s newsletter in the description. The Modern Coder’s longer video on the same question is linked as a second view.
Where this fits
Next is the situation that makes you want to switch branches while your edits aren’t ready to commit: stashing unfinished work.
Check yourself
You merge main into your feature branch and rebase your feature onto main. What is the same, and what differs?
The final file contents are the same. Merge adds a merge commit and keeps your original commit IDs, while rebase makes a straight line of new commits with new IDs.
What is the single question that decides between merge and rebase?
Has anyone else got these commits? If yes, merge. If they exist only on your machine, rebase is safe.
Why is rebasing a shared branch a problem?
The rebased commits have new IDs, so other people's copies of the old commits no longer match yours. When they pull, Git sees two different histories and merging them can conflict or duplicate work.
What does a merge commit give you that rebase cannot?
A permanent record that two lines of work were joined, and the original commits untouched. Rebase gives a cleaner list but loses the record of the branch.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.