Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Concepts

Merge versus rebase

Compare git merge and git rebase: what each does to history, when to choose one, the golden rule of rebasing and how to combine them in a workflow.

git merge and git rebase both bring the work of one branch into another, but they leave different histories. Merge ties two lines of development together with a new commit that has two parents; nothing existing is changed. Rebase takes your commits and replays them on top of another commit, creating new commits with new ids and leaving a straight line.

Neither is "better". Merge records what really happened; rebase tells a cleaner story. The one hard rule is the golden rule of rebasing: never rebase commits that other people already have, because rebasing rewrites them.

Terminal
git switch feature && git merge main      # merge main into feature
git switch feature && git rebase main     # replay feature on top of main

Try it

Bash The same work, merged and rebased

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Fast-forwarding after a rebase

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Choosing between them

Merge Rebase
Existing commits untouched replaced by copies
History shape branches and merge commits linear
Conflicts resolved once, in the merge commit possibly once per replayed commit
Safe on shared branches yes no, unless everyone agrees
Undo git reset --hard ORIG_HEAD git reset --hard ORIG_HEAD or the reflog
Bisecting works, merge commits add noise easiest, every commit is on one line

Usage notes

  • Golden rule. Rebase only commits that exist only in your repository. After you have pushed and someone based work on them, rewriting them forces everyone to repair their copy.
  • Typical team flow. Rebase your private feature branch onto the latest main to keep it current, then merge it into main (often with --no-ff to keep the feature visible as a unit).
  • git pull --rebase replays your unpushed commits on top of the fetched upstream instead of creating a merge commit. Set pull.rebase to make it the default (see pull.rebase).
  • Rebase keeps patches, not snapshots. Each replayed commit applies the same change to a new base, so a commit that built before may not build now. Test after rebasing.
  • Conflicts during rebase show "ours" and "theirs" reversed compared with merge: "ours" is the branch you are rebasing onto.
  • Merging main into a feature branch repeatedly creates noisy "Merge branch main" commits. Rebase for that purpose, or merge only when the branch is shared.
  • git rebase --rebase-merges keeps merge commits instead of flattening them.