Software Engineeringunpacked

Git merge vs rebase: how each one combines branches, the golden rule of rebasing, and which to use when

Git merge and git rebase both bring the work of one branch into another, and both end with the same code. The difference is the history. Merge joins the two branches with a new merge commit and keeps every commit exactly as it happened. Rebase replays your commits one by one on top of the other branch, creating brand new commits, so the history becomes one straight line. The golden rule from the Pro Git book: never rebase commits that exist outside your repository and that other people may have based work on. Rebase your own local work; merge what is already shared.

Video · Git merge vs rebase: what's the difference? (in 1 minute) · 0:47

Git merge vs rebase, in one paragraph

Prefer to watch? The one-minute Short shows the core idea with diagrams; this guide goes further.

You have been working on a branch, and meanwhile someone added new commits to main. Now you want both sets of changes together. Git gives you two tools for that: merge and rebase.

Think of two people writing chapters of the same book. Merging is stapling both stacks together with a note on top that says "joined here". Rebasing is retyping your chapters so they start after the latest page of the other stack, as if you had begun writing today. Either way the book holds the same text. Only the story of how it was written is different.

The starting point: two branches that moved apart

Every commit in Git points to its parent, so a branch is a chain of dots. Here is the situation both commands start from. Your branch split from main at commit B; since then main got C and D, and you made E and F.

A---B---C---D        main
     \
      E---F          your branch

Neither branch has the other's new work. To combine them, Git first finds the common ancestor, the last commit both share (B here).

How git merge works

When you run git merge from main, Git does what the Pro Git book calls a three way merge. It looks at three snapshots: the tip of main (D), the tip of your branch (F) and their common ancestor (B). It combines the changes made on each side since B and records the result in a new commit, M.

A---B---C---D---M    main
     \         /
      E---F---'      your branch

M is a merge commit, and it is special: it has two parents, D and F. Nothing that existed before is changed. Commits C, D, E and F keep their IDs, and the graph shows honestly that two lines of work happened in parallel and were joined.

git switch main
git merge my-branch

There is one shortcut. If main has no new commits since your branch started, there is nothing to combine, so Git simply moves the main pointer forward to your last commit. That is a fast forward merge, and it creates no merge commit at all.

How git rebase works

Rebase takes the other route. Run from your branch, git rebase main does this:

  1. Find the common ancestor (B).
  2. Save the change each of your commits introduced (E, then F).
  3. Move your branch to the tip of main (D).
  4. Apply your saved changes one by one, making a new commit for each.
A---B---C---D---E'---F'   your branch

E' and F' carry the same changes as E and F, but they are new commits: their parent is different, so their IDs (SHA-1 hashes) are different too. The old E and F are no longer part of the branch. Now main can take your work with a fast forward merge, and the history reads as one straight line, as if everything happened in order.

git switch my-branch
git rebase main
git switch main
git merge my-branch      # a fast forward now

The Pro Git book stresses the key point: the final snapshot is the same either way. Merge and rebase produce identical code; only the history is different.

Merge vs rebase at a glance

git merge git rebase
What it does joins branches with a merge commit replays your commits on top of the other branch
Existing commits unchanged, same IDs replaced by new commits with new IDs
History shape shows the parallel lines and the join one straight line
Conflicts resolved once, in the merge commit may come up once per replayed commit
Safe on shared commits yes no (see the golden rule)
Best for branches other people already have tidying your own local work

The golden rule of rebasing

The Pro Git book gives one rule that saves a lot of pain:

Do not rebase commits that exist outside your repository and that people may have based work on.

Here is why. Say you pushed E and F, and a teammate pulled them and built G on top of F. You then rebase, so E and F are replaced by E' and F', and you force push. Your teammate's G still points to the old F. When they pull, Git sees two versions of the same changes, and they may end up merging the old commits back in, with duplicates in the history and a confusing cleanup for everyone.

The book's own advice for getting the best of both: rebase local changes before you push to clean up your work, but never rebase anything you have pushed somewhere others use.

If you rebase a branch that only you work on and have already pushed (for example, your own pull request branch), you will need to force push. Use git push --force-with-lease rather than plain --force: it only overwrites the remote branch if nobody else has pushed to it since you last fetched. (Your remote is just another copy of the repository on a server, reached over HTTPS or SSH like any other request across the internet.)

When to use merge and when to use rebase

Use rebase when:

Use merge when:

Many teams combine them: each developer rebases their own branch onto the latest main while working, and the finished branch is merged through a pull request. If your team runs checks on every pull request, for example Claude Code in GitHub Actions or a test suite like the ones in our guide to types of software testing, a fresh rebase before review means the checks run against the code that will actually land.

Handling conflicts in a merge or a rebase

Both commands stop when the same lines changed on both sides. The way out differs a little.

During a merge, you fix the conflicted files once, stage them and commit. That single merge commit records your resolution.

During a rebase, Git replays your commits one at a time, so a conflict can stop it at any of them. Fix the files, stage them, and continue. If it gets messy, you can always go back to where you started.

git add <fixed files>
git rebase --continue    # move on to the next commit
git rebase --abort       # give up and restore the branch

Because a rebase can stop several times, rebasing a long branch with many commits that touch the same code can be slower to resolve than one merge. That is a fair reason to prefer merge for long lived branches.

Interactive rebase: tidying your own history

Rebase has a second use: rewriting your own commits before anyone sees them. git rebase -i opens a list of your recent commits, and you can reorder them, squash several "fix typo" commits into one, edit a message or drop a commit. It is a great way to turn a messy work session into a few clear commits for review. The golden rule still applies: do it before you push, not after others have the commits.

The same idea of keeping your work reviewable runs through our Claude Code best practices: small, clear steps are easier for people (and tools) to check.

The short version

Sources: Pro Git, Rebasing · Pro Git, Basic Branching and Merging · git rebase documentation · git pull documentation · git push documentation

FAQ

What is the difference between git merge and git rebase?

Git merge combines two branches by creating a merge commit with two parents, and keeps the existing commits unchanged. Git rebase takes the commits of your branch and replays them on top of another branch as new commits with new IDs, giving a straight, linear history. The final code is the same; only the history differs.

Is it better to merge or rebase?

Neither is better everywhere. Rebase gives a clean, linear history and suits work that only exists on your machine. Merge is safe for anything already shared, because it never rewrites existing commits. A common habit is to rebase local work before pushing and to merge shared branches.

What is the golden rule of rebasing?

The Pro Git book puts it as: do not rebase commits that exist outside your repository and that people may have based work on. Rebasing replaces commits with new ones, so anyone who built on the old commits ends up with duplicate or conflicting history.

Does git rebase change commit hashes?

Yes. Rebase creates new commits that carry the same changes but have a different parent, so they get new commit IDs (SHA-1 hashes). The old commits are no longer on the branch, which is why rebasing shared commits causes trouble for other people.

What does git pull --rebase do?

It fetches the latest commits from the remote and then rebases your local commits on top of them, instead of creating a merge commit. You can make it the default with git config pull.rebase true.