Merge or rebase?

Topic 5 of 5 in Rewriting history7 min readBeginnerFree course

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.

Merge or rebase? Merge gives true history: a1, 7d, then b2 and e4 in parallel, joined by merge commit M. Pros: it shows when work happened in parallel and never rewrites shared commits; con: a busy graph with many merge commits. Rebase gives clean, linear history: a1, 7d, b2, e4′. Pros: easy to read and to bisect, no extra merge commits; con: it rewrites commits, which get new IDs. Golden rule: never rebase commits that other people already have. Rebase your own local work freely.

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 bisect and git log are simpler on linear history.
  • Cleaner pull requests. A branch rebased onto the latest main shows 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:

  1. Update your feature branch by rebasing onto the latest main (git pull --rebase, or git fetch then git rebase origin/main). Your branch stays a clean line on top of main.
  2. Tidy it with interactive rebase before review, if needed.
  3. 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.

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 deserves a special mention, because many teams use it for everything:

  • main gets 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 --abort or git reset --hard ORIG_HEAD gets 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.

Exercise

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.

Terminal
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 histories

Summary

  • 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.