Merge or rebase?
Last updated
People argue about merging and rebasing as if one were right. Each keeps something and loses something. In this lesson you'll compare the same work brought in both ways, then sort six situations by which is the usual choice, and why.
By the end of this lesson you can
- Say what merging keeps that rebasing loses, and the reverse
- Choose merge or rebase for a situation, with a reason
- Explain why a team's agreement matters more than the choice
The same work, both ways
pancit-canton has two commits; main has had one since. Brought in with a merge:
* 52633c7 (HEAD -> main) Merge branch 'pancit-canton' |\ | * cf121f8 Add shrimp to pancit canton | * a1f23d8 Add pancit canton recipe * | cf91abf Welcome corrections in the README |/ * 67e435d Cover the leche flan with foil
Brought in with a rebase, then a fast-forward:
* 3735212 (HEAD -> main) Add shrimp to pancit canton * 4e93991 Add pancit canton recipe * cf91abf Welcome corrections in the README * 67e435d Cover the leche flan with foil
The files at the end are identical. Only the history differs.
What each keeps and loses
Merging keeps history as it happened: the branch really did start from an older main, and the merge commit records when the two came together. Nothing is rewritten, so it's always safe on shared branches. The cost: loops, and merge commits that can clutter a busy log.
Rebasing gives a straight line, as if the work had been done in order, which is easier to read and to bisect (lesson 8.4). The cost: the commits are new copies (their dates and parents no longer show how the work really went), and it's only safe on commits nobody else has.
Many teams combine them: rebase your own branch to keep it current and tidy, then merge it (through a pull request) into main. Either way, what matters most is that the team agrees and writes it down, often in CONTRIBUTING.md, and sets the host's merge buttons to match (lesson 5.2).
Sort the situations
0 of 6 placed right
Your own feature branch, not pushed yet, is behind main. You want it up to date before opening a pull request.
Joaquin pushed to main while you made one small commit on main. You pull.
The desserts branch, which Ana and Carla both push to, needs main’s latest changes.
A reviewed pull request is ready to go into main, and your team wants each feature visible as a loop.
Your pull request has been open a week and others have reviewed it. main has moved on; there’s no conflict.
Your seven messy local commits need tidying into three before anyone sees them.
Spot the trap: mixing both on one branch
On the shared desserts branch, Ana sometimes pulls with merge and sometimes with rebase, and Carla does the opposite. Nobody agreed on anything.
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Pro Git: Rebase vs. Merge: history as a record, or as a story
- Git documentation: git-merge: merge commits and --no-ff
- GitHub Docs: About merge methods on GitHub: how a repository chooses which merge buttons to allow