Merge or rebase?
Overview
You now know two ways to combine work: merge and rebase. Developers have strong opinions about which is better. In reality both are useful, and the right choice depends on what you want your history to show. This lesson compares them and gives you a sensible default.
Same result, different history
The most important fact first: when there are no conflicts (or you resolve them the same way), merge and rebase produce exactly the same files. The exercise proves it — the final tree IDs are identical. What differs is the history:
| Merge | Rebase | |
|---|---|---|
| Shape | shows branches and merge points | a single straight line |
| Commits | original commits, plus a merge commit | rewritten copies of your commits |
| Records | what actually happened, in parallel | what the result looks like, as if done in sequence |
| Rewrites history | no | yes |
| Safe on shared branches | yes | no |
| Conflicts | resolved once, in the merge commit | resolved per commit as they’re replayed |
Arguments for merging
- It’s honest. History shows that two lines of work happened in parallel, and when they were joined.
- It’s safe. It never rewrites existing commits, so it can’t disrupt anyone else.
- Context is kept. With
--no-ff, a merge commit groups a feature’s commits together, and reverting the merge reverts the whole feature.
The downside: on a busy project, the graph becomes a tangle of lines and “Merge branch ‘main’ into feature” commits that make git log hard to read.
Arguments for rebasing
- Readable history. A straight line of meaningful commits reads like a story.
- Easier debugging. Tools like
git bisectandgit logare simpler on linear history. - Cleaner pull requests. A branch rebased onto the latest
mainshows reviewers only its own changes, and merges as a fast-forward.
The downside: rebasing rewrites commits. On a branch others are using, that causes duplicates and confusion — and resolving the same conflict in several replayed commits can be tedious.
The golden rule
Never rebase commits that exist outside your own repository and that others may have based work on.
That boils down to: rebase your own feature branch freely, never rebase main or anyone else’s branch.
A sensible default for teams
Most modern teams settle on something like this:
- Update your feature branch by rebasing onto the latest
main(git pull --rebase, orgit fetchthengit rebase origin/main). Your branch stays a clean line on top ofmain. - Tidy it with interactive rebase before review, if needed.
- Integrate through a pull request, using whichever merge method the repository allows:
- Squash and merge — the whole PR becomes one commit on
main. Simple linear history; individual commits are kept in the PR on GitHub. - Rebase and merge — your commits are replayed onto
main, no merge commit. - Create a merge commit — classic merge, keeps branch structure.
- Squash and merge — the whole PR becomes one commit on
On GitHub, the repository’s settings decide which of those buttons exist, so the team’s choice is enforced automatically. Branch rules can also require linear history, which blocks merge commits on main entirely.
Squash merging: the popular middle ground
Squash merging deserves a special mention, because many teams use it for everything:
maingets one commit per pull request, with the PR title as its message.- Developers don’t need to polish their branch’s individual commits.
- Reverting a feature is reverting one commit.
The cost is that the branch’s individual commits aren’t on main — only in the pull request. For small and medium pull requests, that’s usually fine.
Practical tips
- Set a pull default once, so you’re not asked every time:
git config --global pull.rebase true(rebase your local commits when pulling) — see Pushing and pulling. - If a rebase goes badly,
git rebase --abortorgit reset --hard ORIG_HEADgets you back. - If someone accidentally rebased a shared branch, don’t panic: the reflog on any machine that had the old commits can restore them.
In one sentence: rebase to keep your work up to date and tidy; merge (or squash-merge) to bring finished work into the shared branch.
Same work, two histories
The script builds the same starting point twice, in merge-repo and rebase-repo: main and feature/cart have each moved on by one commit. In merge-repo, integrate the feature with a merge commit. In rebase-repo, rebase the feature onto main and fast-forward. Compare: both repositories must end with identical files, but different histories — count the commits and merge commits in each.
setup() {
mkdir "$1" && cd "$1" && git init -qb main
echo "shop" > app.txt && git add . && git commit -qm "Start shop"
git switch -qc feature/cart
echo "cart" > cart.txt && git add . && git commit -qm "Add cart"
git switch -q main
echo "# Shop" > README.md && git add . && git commit -qm "Add README"
cd ..
}
setup merge-repo
setup rebase-repo
# 1. merge-repo: merge commit
# 2. rebase-repo: rebase + fast-forward
# 3. Compare files and historiessetup() {
mkdir "$1" && cd "$1" && git init -qb main
echo "shop" > app.txt && git add . && git commit -qm "Start shop"
git switch -qc feature/cart
echo "cart" > cart.txt && git add . && git commit -qm "Add cart"
git switch -q main
echo "# Shop" > README.md && git add . && git commit -qm "Add README"
cd ..
}
setup merge-repo
setup rebase-repo
# 1. merge-repo: merge commit
(cd merge-repo && git merge --no-edit feature/cart)
# 2. rebase-repo: rebase + fast-forward
(cd rebase-repo && git switch -q feature/cart && git rebase main && git switch -q main && git merge --ff-only feature/cart)
# 3. Compare files and histories
[ "$(cd merge-repo && git rev-parse HEAD^{tree})" = "$(cd rebase-repo && git rev-parse HEAD^{tree})" ] && echo "identical files"
(cd merge-repo && git log --oneline --graph) # 4 commits, 1 merge
(cd rebase-repo && git log --oneline --graph) # 3 commits, straight lineSummary
- Merge and rebase produce the same final files; they differ only in how history is recorded.
- Merge preserves exactly what happened, including parallel work, at the cost of merge commits.
- Rebase gives a clean, linear history, but rewrites commits — so never on shared branches.
- A common team policy: rebase your own branch to update it, merge (or squash-merge) pull requests into main.
- Pick one policy as a team and enforce it with repository settings.