Merge vs Rebase

Updated · 10 min read
mediumGit 2.53gitmerge-vs-rebasegolden-ruleworkflowinterview

Merge and rebase are the two ways in Git to bring the changes of one branch into another. git merge joins the two lines of work with a merge commit and keeps every commit as it was. git rebase replays one branch's commits on top of the other, so the history becomes one straight line. Both give the same files in the end. They differ in the history they leave behind and in when they are safe to use.

Think of two ways to record a project's diary. One keeps every page exactly as written, with a note "here the two teams' diaries were joined" (merge). The other rewrites the team's pages afterwards so they read as one continuous story (rebase). The first is honest about what happened. The second is easier to read. Neither is wrong, but you must never rewrite pages that other people have already copied.

The Same Situation, Two Ways

Navika's feature branch has two commits, and main has moved on by one. To compare fairly, she makes two identical copies of the feature branch, merges main into one and rebases the other:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
git add .
git commit --quiet -m "Add README and the term fee"
git switch --quiet -c feature-merge
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet -m "Add receipt numbers"
echo "Print after payment" >> receipts.txt
git commit --quiet -am "Print a receipt after payment"
git branch feature-rebase
git switch --quiet main
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
git log --oneline --graph --all
Output
* 95eeef0 (HEAD -> main) Add the hostel fee
| * da357cb (feature-rebase, feature-merge) Print a receipt after payment
| * c7de0dd Add receipt numbers
|/
* 4988b51 Add README and the term fee

Merge main into the first copy:

Terminal
git switch --quiet feature-merge
git merge --quiet --no-edit main
git log --oneline --graph
Output
*   8850973 (HEAD -> feature-merge) Merge branch 'main' into feature-merge
|\
| * 95eeef0 (main) Add the hostel fee
* | da357cb (feature-rebase) Print a receipt after payment
* | c7de0dd Add receipt numbers
|/
* 4988b51 Add README and the term fee

Rebase the second copy onto main:

Terminal
git switch --quiet feature-rebase
git rebase --quiet main
git log --oneline --graph
Output
* c5c99b6 (HEAD -> feature-rebase) Print a receipt after payment
* f8a445a Add receipt numbers
* 95eeef0 (main) Add the hostel fee
* 4988b51 Add README and the term fee

Explanation: the merge kept both receipt commits with their original IDs and added a merge commit that joins the two lines. The rebase made new copies of the receipt commits on top of main, with new IDs and no merge commit. And the files? Exactly the same:

Terminal
git diff --quiet feature-merge feature-rebase && echo "The two branches have identical files"
Output
The two branches have identical files

git diff --quiet prints nothing and only reports whether there is a difference, so the echo runs only when there is none.

The same branch, merged and rebased

Comparison Table

MergeRebase
HistoryShows the real shape: parallel lines joined by merge commitsOne straight line, as if the work had been done in sequence
Existing commitsKept as they are, with the same IDsReplaced by copies with new IDs
Extra commitsA merge commit each timeNone
ConflictsResolved once, for the whole mergeResolved per commit, possibly several times
Safe on shared branchesYesNo, unless everyone agrees
Undo a whole featuregit revert -m 1 <merge> in one stepRevert each commit, or a range
git log readabilityCan get busy with many mergesVery easy to read
Shows when work was integratedYesNo

The Golden Rule of Rebasing

The reason is the new IDs. Suppose Navika had pushed feature-rebase before rebasing it. Her local branch now has different commits from the server's branch, so a normal push is rejected:

Terminal
git init --bare --quiet ../fee-tracker.git
git remote add origin ../fee-tracker.git
git push --quiet origin feature-merge:feature-rebase
git push origin feature-rebase
Output
To ../fee-tracker.git
 ! [rejected]        feature-rebase -> feature-rebase (non-fast-forward)
error: failed to push some refs to '../fee-tracker.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

(The first push puts the un-rebased history on the server under that name, to recreate the situation.) non-fast-forward means the server's branch is not contained in hers anymore. Pushing would need a force push, and anyone who had pulled the old commits would now have commits that no longer exist on the server, with their own work built on top. That is what the golden rule prevents.

In practice, the rule becomes:

  • Rebase freely what is only on your computer, and your own feature branch, even if you pushed it for a pull request that nobody else commits to. Then push with --force-with-lease.
  • Never rebase main, develop, release branches, or any branch a teammate also commits to. Use merge (or revert) there.

How Teams Combine Them

Most teams use both, each where it fits:

SituationUsual choiceWhy
Updating your local main from the servergit pull --rebase (or pull.rebase true)Avoids pointless "Merge branch 'main' of ..." commits
Bringing main into your own feature branchgit rebase mainKeeps the pull request a clean list of your commits
Bringing main into a branch others also usegit merge mainDoesn't rewrite shared commits
Finishing a pull requestPlatform setting: merge commit, squash or rebaseTeam convention, applied consistently
Cleaning up your commits before reviewInteractive rebase (git rebase -i)Covered in "Interactive Rebase"

A short way to remember it: rebase to update your own work, merge to combine shared work.

Common Mistakes

Choosing one and banning the other. Each has its place. Agree on where each is used.

Rebasing a shared branch to "clean it up". Everyone else's copy breaks. Merge, or ask the whole team first.

Merging main into a feature branch every hour. The branch fills with merge commits. Rebase your own branch instead, or merge less often.

Thinking rebase loses information. It drops the information "these commits were made in parallel" and the original commit dates of integration, which some teams value. Decide whether you need it.

Resolving the same conflict again and again during a long rebase. If many commits touch the same lines, a merge (one resolution) can be easier. git rerere, covered with merge strategies, can also remember resolutions.

Under the Hood

Both commands use the same three-way machinery. A merge runs it once, with the two branch tips and their merge base, and records the result in a commit with two parents. A rebase runs it once per commit, applying each commit's change onto the new base, and records each result as an ordinary one-parent commit. That is why the final files match: the same changes are combined, only grouped differently in history. It is also why a rebase can ask you to resolve conflicts several times, once for each commit that touches the conflicting lines.

Interview Corner

What is the difference between merge and rebase?

Both integrate changes from one branch into another. Merge creates a merge commit and keeps all existing commits, preserving the real, parallel history. Rebase replays the branch's commits on top of the other branch as new commits, producing a linear history without merge commits.

When should you use rebase, and when merge?

Rebase to update your own, unshared work (local commits, your own feature branch) and to keep history linear. Merge to integrate shared branches, or whenever rewriting commits could affect others.

What is the golden rule of rebasing?

Don't rebase commits that have been pushed to a shared branch that others may have based work on, because rebasing rewrites those commits with new IDs and makes everyone else's history diverge.

Do merge and rebase produce the same code?

Yes, given the same conflict resolutions, the final files are identical. Only the commit history differs.

What are the downsides of rebasing?

It rewrites commits (new IDs), which is unsafe on shared branches and requires force pushes for your own pushed branches, it may require resolving the same conflict for several commits, and it hides when work was actually integrated.

Practice

Challenge: count the commits. In the same repository, compare the two branches: how many commits does each have in total? Print the count for feature-merge and for feature-rebase with git rev-list --count.

What does Git print? Before you open the solution, count the commits in each graph from the examples above.

Show solution
Terminal
git rev-list --count feature-merge
git rev-list --count feature-rebase
Output
5
4

feature-merge has five commits: the first commit, the hostel fee, the two receipt commits and the merge commit. feature-rebase has four: the same work, without a merge commit. Same files, one commit more for the merge.

Checkpoint. In a practice repository, repeat the experiment of this lesson with a conflict between the feature and main. Count how many times you resolve the conflict with merge and with rebase, and write one sentence on which history you prefer to read.

Quick check: Merge vs rebase

4 questions
1Which one keeps the original commit IDs?
2Which one produces a straight history without merge commits?
3Which branch must you not rebase?
4After merging and rebasing the same work, how do the files compare?

Key Takeaways

Quick reference

CommandIt is used to
git merge mainIntegrate main, keeping history as it is
git rebase mainReplay your branch on top of main
git pull --rebaseUpdate a branch without merge commits
git config --global pull.rebase trueMake pulls rebase by default
git push --force-with-leasePush your own rebased branch
git revert -m 1 <merge>Undo a merged feature in one step

Was this tutorial helpful?

Lesson 55 of 106