Course contents · Lesson 11 of 17

Git Merge vs Rebase: Which One and When?

Git and Collaboration · Lesson 11 of 17 · 11 min Engineer

Your pull request adds a contact form on a branch called feature, but while you worked, a teammate's fix was merged into main. Before your work joins main, the two lines of history must come together, and Git offers two ways. This lesson settles merge vs rebase: what each does to your history, how to spot each in a graph, and when rebasing is unsafe.

Merge: join two lines with a merge commit

To merge is to bring another branch's work into the branch you are on. As in the last lesson's diagram, your branch left main at commit B and gained X and Y, while main gained C. Switch to the branch that receives the work, then name the one to bring in:

git switch main
git merge feature

Both lines have new commits, so Git creates a merge commit, the commit with two parents you met in Branches and Pull Requests. In a terminal, Git first opens your editor with the message Merge branch 'feature'; save and close it. Then it names the strategy, its method for combining files (ort since Git 2.34, recursive before), and lists the changed files (trimmed here):

Merge made by the 'ort' strategy.

A merge changes nothing that already exists. X, Y and C keep their hashes, and the history records exactly what happened, including that two lines of work ran side by side.

If main had no new commits, it would be an ancestor of feature, a commit in its history, and there would be nothing to join. Git would do a fast-forward instead, moving main up to Y, as git pull did in Git Basics. If both branches changed the same lines, Git stops with a merge conflict, the subject of the next lesson, and git merge --abort puts everything back.

Rebase: replay your commits on top of main

To rebase is to give your branch a new starting point. Git takes the commits that are on your branch but not on main, sets their changes aside, and reapplies them one at a time on top of main's newest commit. It uses your local main, so git pull on main first, then run it from your branch:

git switch feature
git rebase main

When every commit applies cleanly, Git confirms on one line, where refs/heads/feature is Git's full name for your branch:

Successfully rebased and updated refs/heads/feature.

If a replayed commit clashes with a change on main, Git stops at that commit and reports a conflict instead. git rebase --abort returns your branch to where it was. If it prints Current branch feature is up to date., you probably skipped that pull.

Follow the four steps in the diagram. Git cannot move X, because a commit's hash is calculated from its contents, which include its parent, as What Is Git? showed. X must now sit on C, so Git writes a new commit, X', and then Y' on top of it, with the same changes and messages but new hashes.

Four numbered steps joined by arrows, for git rebase main run on feature. Step 1, feature branches from B: main has A and B, and feature has X and Y. Step 2, main moves on to C, a teammate's work. Step 3, rebase sets X and Y aside: an arrow leads from the old X and Y, drawn faded, to a dashed temporary area holding their changes. Step 4, rebase replays them after C: one straight line A, B, C, X prime, Y prime, with main on C and feature on Y prime, labelled one straight line, new hashes
Rebase never moves a commit: it replays the changes after C as new commits, X' and Y'.

Your branch now starts at the tip of main, so main is its ancestor. Running git merge feature on main is then a fast-forward: Git prints Fast-forward and moves main to Y', with no merge commit.

Side by side: shape, readability and hashes

Here is the same work done both ways: compare the shapes, then the hashes.

Two panels of the same work. Left, git merge feature: main runs A, B, C to M, and the feature commits X and Y leave B and rejoin at M, a merge commit with parents C and Y, forming a diamond; X and Y keep their hashes 9e1d0c4 and b7c2e15. Right, git rebase main: one straight line A, B, C, X prime, Y prime with new hashes 5b7a2f1 and d3e8c90, main on C and feature on Y prime; the old X and Y are drawn faded as left behind. A note says the hashes are illustrative
Merge adds one commit and keeps every hash; rebase gets its straight line by replacing your commits.

The table sums up the differences:

MergeRebase
Graph shapeTwo lines joined by a merge commitOne straight line
Reading itShows parallel work; busy on big teamsReads in order; hides parallel work
Your commitsUnchanged, same hashesReplaced by copies, new hashes
ConflictsResolved once, in the mergeResolved per replayed commit

Neither is better everywhere. The Pro Git book describes two views of history: a record of what actually happened, which merge keeps, or a story of how the project was built, which rebase lets you tidy.

The golden rule: never rebase commits others have pulled

Because rebase replaces commits, Pro Git gives one rule: do not rebase commits that exist outside your repository and that people may have based work on.

Say Ravi pulls your feature branch, with X and Y, and commits on top. You rebase and replace the server's X and Y with X' and Y'. Ravi's work now sits on commits the branch no longer contains, and when he pulls, he can end up with the same changes twice: X and Y beside X' and Y'.

Git guards against this by default. After you rebase a branch you already pushed, a plain git push is rejected as (non-fast-forward), because accepting it would drop the old X and Y from the server's branch. Overwriting them takes a force push, a push that replaces the server's branch even if commits are lost.

If the branch is your own pull request and nobody else has pulled it, many teams accept a rebase. Push it with --force-with-lease, a force push that refuses to overwrite the server's branch if it has commits you have not fetched:

git push --force-with-lease

The + and the three dots mark a forced update from the old tip to the new one (trimmed, example hashes):

 + b7c2e15...d3e8c90 feature -> feature (forced update)

If it is rejected with (stale info), someone pushed to your branch since your last fetch: fetch, look at their commits, and do not overwrite them. Never force-push a shared branch such as main. The flowchart turns the rule into one question.

Flowchart. Start: main moved on while you worked. Decision: has anyone else pulled this branch? Yes leads to merge, git merge main into your branch, then shared commits stay valid and teammates can keep pulling. No leads to rebase is fine, git rebase main, only your copy changes, then fast-forward main with git merge feature on main, no merge commit. A banner says not sure? Merge. It rewrites no commits
If nobody else has your commits, rebase and fast-forward; if someone does, merge.

What teams choose: merge commit, squash or rebase merge

On GitHub, a pull request's merge button offers three methods, described in GitHub's guide, and a repository can allow only the ones its team uses:

  • Create a merge commit, the default: adds the branch's commits plus a merge commit. It works like git merge --no-ff, where --no-ff makes a merge commit even when a fast-forward is possible.
  • Squash and merge: combines all the pull request's commits into one new commit on main: tidy, but your branch's own commits never reach main.
  • Rebase and merge: adds each commit to main on its own, with no merge commit. GitHub always creates new commits for this, so the hashes differ from your branch's.

What matters most is consistency. A history mixing merge commits, squashed commits and rebased runs is harder to read than any one style, so teams pick one and write it down.

A team that wants a straight line can turn on the branch protection setting Require linear history, which blocks merge commits, so pull requests must use squash or rebase merges.

Check yourself

After git rebase main, your commit messages look the same but every hash has changed. Did something go wrong?

No. Rebase writes new commits on a new parent, and a hash is calculated from a commit's contents, including its parent, so new hashes are expected.

Ravi has pulled your branch and added commits on top. Should you rebase it onto main to tidy the history?

No, merge main into it instead. A rebase would replace the commits Ravi built on, and his next pull could bring back the same changes twice.

main has not moved since you branched. Why does git merge feature on main create no merge commit?

There is nothing to join: main is an ancestor of your branch, so Git fast-forwards it to your newest commit. A merge commit is needed only when both lines have new commits, or when you ask for one with --no-ff.