Sub topic 3
Fast-forward merges and merge commits
This chapter assumes you can create and switch branches, make commits, and read git log output. It also assumes you know what a branch is: a movable name that points at one commit. Earlier chapters covered what git merge does and how the merge base works. This chapter covers the two shapes a successful merge can take, how to choose between them, and where conflicts can and cannot occur.
Three outcomes of git merge
When you run git merge feature on main, Git compares the tip of main with the tip of feature. One of three things happens:
- Already up to date. The tip of
featureis already part ofmain's history. Git printsAlready up to date.and changes nothing. - Fast-forward. The tip of
mainis an ancestor offeature's tip. Git moves themainpointer forward. No new commit is created. - True merge. The two branches have each gained commits since they diverged. Git combines the changes and records a new merge commit with two parents.
Fast-forward merges
A fast-forward is possible when your branch has not moved since feature was created. The history is a straight line, so there is nothing to combine. Git only has to advance the pointer and update your working tree.
git init -b main ff-demo && cd ff-demo
printf 'one\n' > notes.txt
git add notes.txt && git commit -m "Add notes"
git switch -c feature
printf 'two\n' >> notes.txt
git commit -am "Add line two"
git switch main
git merge featureThe output includes the word Fast-forward and a diffstat. Afterward, main and feature point at the same commit, and git log --oneline shows one straight line. The history gives no sign that a branch ever existed.
A fast-forward can never produce a conflict. Conflicts come from combining two sets of changes, and a fast-forward combines nothing.
Merge commits
If main also gained a commit while you worked on feature, the branches have diverged and no pointer move can join them. Git performs a three-way merge using the merge base and records the result as a merge commit.
git init -b main merge-demo && cd merge-demo
printf 'one\n' > a.txt
git add a.txt && git commit -m "Add a"
git switch -c feature
printf 'feature\n' > b.txt
git add b.txt && git commit -m "Add b on feature"
git switch main
printf 'main\n' > c.txt
git add c.txt && git commit -m "Add c on main"
git merge feature -m "Merge feature into main"
git log --graph --oneline
git show --no-patch --format=%P HEADThe graph shows the history splitting and rejoining:
* 3f2a1b9 (HEAD -> main) Merge feature into main
|\
| * 8c4d7e0 (feature) Add b on feature
* | b61e9a2 Add c on main
|/
* 5d0c3f4 Add aThe hashes will differ on your machine. The last command prints two hashes, one per parent. The first parent is the branch you were on when you ran the merge (main). The second is the tip of the branch you merged in. A merge commit is the only ordinary commit with more than one parent.
Here the two branches touched different files, so Git merged them without help. If both had changed the same lines, the merge would have stopped with a conflict. That is the case the rest of this tutorial deals with.
Choosing the shape yourself
Git picks fast-forward whenever it can. You can override that with flags or configuration.
# Always record a merge commit, even when a fast-forward is possible
git merge --no-ff feature -m "Merge feature into main"
# Refuse to create a merge commit; fail unless a fast-forward is possible
git merge --ff-only featureWhen --ff-only cannot fast-forward, Git exits with fatal: Not possible to fast-forward, aborting. and leaves your branch untouched. That makes it a safe default for automation. The failure tells you the branches diverged, and you decide whether to merge or rebase.
You can set these as defaults:
git config --global merge.ff false
git config --global pull.ff onlymerge.ff false is the same as always passing --no-ff. merge.ff only is the same as --ff-only. pull.ff only makes git pull fail instead of silently creating a merge commit on a diverged branch.
Trade-offs
| Approach | History it produces | Strengths | Costs |
|---|---|---|---|
| Fast-forward | Linear, no trace of the branch | Simple log, easy bisecting | Loses the record that the work was a unit |
| `--no-ff` merge commit | Branch shape stays visible | One commit to revert a whole feature, clear grouping | More noise in `git log`, extra commits with no new content of their own |
| `--ff-only` | Linear, enforced | Never creates surprise merge commits | Fails when branches diverge, so you must rebase first |
Teams that want to revert a feature in one step often use --no-ff. A merge commit can be undone with git revert -m 1 <merge-commit>, where -m 1 says to keep the first parent's side. Teams that prefer a linear log often rebase first and then use --ff-only.
Edge cases and gotchas
- A fast-forward leaves nothing to abort.
git merge --abortonly works while a merge is unfinished, which is a state that arises from conflicts. A fast-forward finishes instantly. --no-ffand conflicts. With--no-ff, Git still cannot conflict on a case that would have fast-forwarded. The merge commit has the same content as the branch tip, so it has no overlapping changes to resolve.- Squash is not a merge.
git merge --squash featurestages the combined changes but creates no merge commit. The nextgit commithas a single parent, so Git does not remember thatfeaturewas merged. Mergingfeatureagain later can then produce extra conflicts. - Merging in the other direction. Merging
mainintofeaturefast-forwards only iffeaturehas no commits of its own. Direction matters, because the first parent of the merge commit is the branch you are on. - Merge commits in
git log. Usegit log --first-parentto follow only the first parent. On a branch that receives--no-ffmerges, this shows one line per merged feature.
Summary of what to check
Before merging, ask whether the branches have diverged. If they have not, you get a fast-forward, which cannot conflict. If they have, you get a merge commit, which can conflict. You can change this behavior with --no-ff, --ff-only, and the merge.ff setting. Next, you will look at what happens when Git cannot combine the changes by itself.
Exercise
You are on main and run git merge --ff-only feature. Both branches have one new commit since they diverged. What happens, and what are two ways to continue?
Show answer
Git prints fatal: Not possible to fast-forward, aborting. and changes nothing, because the branches diverged. You can run git merge feature to create a merge commit, which may conflict. Or you can run git rebase main while on feature and then fast-forward main to it.
Exercise
After a merge you run git show --no-patch --format=%P HEAD and it prints a single hash. Was the last merge a fast-forward, a merge commit, or something else, and why?
Show answer
It was not a merge commit, because a merge commit has two parent hashes. HEAD is either the result of a fast-forward, where HEAD is the other branch's tip commit, or a squash commit, which has one parent. Run git log --graph --oneline and check whether the commit message and history match the feature's commits to tell the two apart.