Sub topic 3
Fast-forward merges and merge commits
This chapter assumes you can create commits and branches, switch between them, and read git log. It covers the two shapes a successful merge can take, and how to choose between them.
Two outcomes of git merge
When you run git merge feature, Git compares the tip of your current branch with the tip of feature. The result depends on how the two histories relate.
- If your current branch tip is an ancestor of
feature, Git can move the branch pointer forward. This is a fast-forward merge. - If both branches have new commits since the merge base, Git must combine them. It creates a merge commit.
Fast-forward merges
A fast-forward happens when main has no commits that feature lacks. Git has nothing to combine. It moves the main pointer to the same commit as feature and updates 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 second line"
git switch main
git merge featureGit prints Fast-forward. Running git log --oneline shows a straight line with no merge commit. Because no new commit is created, no conflict can occur.
Merge commits
Now suppose both branches moved on. The next commands add a commit on main that touches a different file from feature.
git switch -c other
printf 'x\n' > other.txt
git add other.txt && git commit -m "Add other file"
git switch main
printf 'y\n' > main-only.txt
git add main-only.txt && git commit -m "Add main-only file"
git merge other -m "Merge other into main"The histories diverged, so Git performs a three-way merge. The changes touch different files, so it combines them and records a merge commit. A merge commit is a commit with two parents. Run git log --oneline --graph to see the history split and rejoin.
If both sides changed the same lines, Git stops here with a conflict. The next chapters cover that case.
Controlling the outcome
By default Git fast-forwards when it can. You can override this.
| Option | Effect |
|---|---|
| `--no-ff` | Always create a merge commit, even when a fast-forward is possible. |
| `--ff-only` | Refuse to merge unless a fast-forward is possible. Git exits with an error otherwise. |
git merge --no-ff feature -m "Merge feature"
git merge --ff-only featureTeams use --no-ff to keep a visible record that a branch existed. They use --ff-only to keep history linear and to catch the case where a branch needs updating first.
Common errors
- Expecting a merge commit and getting none. Git fast-forwarded. Use
--no-ffif you want one. - Running
git merge --ff-onlyand seeingfatal: Not possible to fast-forward, aborting.The branches diverged. Merge normally, or rebase first. - Not seeing a branch in the log after a fast-forward. The commits exist, but the history is a single line, so nothing marks where the branch started.
Exercise
You run git merge feature on main. main has no commits that feature lacks. What does Git do, and how would you force a merge commit anyway?
Show answer
Git fast-forwards: it moves the main pointer to the tip of feature and creates no new commit. To force a merge commit, run git merge --no-ff feature.
Exercise
A teammate's branch has diverged from main. What happens when you run git merge --ff-only on it?
Show answer
Git refuses and exits with "Not possible to fast-forward, aborting." No merge happens, because a fast-forward is impossible when both branches have new commits.