Git Merge vs Rebase: Which One and When?
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.
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.
The table sums up the differences:
| Merge | Rebase | |
|---|---|---|
| Graph shape | Two lines joined by a merge commit | One straight line |
| Reading it | Shows parallel work; busy on big teams | Reads in order; hides parallel work |
| Your commits | Unchanged, same hashes | Replaced by copies, new hashes |
| Conflicts | Resolved once, in the merge | Resolved 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.
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-ffmakes 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 reachmain. - Rebase and merge: adds each commit to
mainon 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.