Fast-Forward vs Merge Commit

Updated · 11 min read
mediumGit 2.53gitfast-forwardno-ffsquashinterview

Two people read the same book. One stopped at page 40, and the other has read on to page 70. To catch up, the first reader doesn't need to combine anything: they just move their bookmark to page 70. But if both had read different chapters, they would have to talk to put the story together.

Merging in Git has the same two cases. If the branch you are on has nothing new since the other branch split off, Git simply moves it forward to the other branch's commit. That is a fast-forward, and no merge commit is needed. If both branches have new commits, Git has to combine them in a merge commit. Teams have opinions about which history they want, and three options let you choose: --no-ff, --ff-only and --squash.

In this lesson, you will see each kind of merge and the history it leaves behind, so you can choose the right one.

A Fast-Forward Merge

Advitha made two commits on feature/late-fee. Nobody committed to main in the meantime:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
git add .
git commit --quiet -m "Add README and the term fee"
git switch --quiet -c feature/late-fee
echo 'Late fee: $250' >> fees.txt
git commit --quiet -a --author="Advitha Anamaneni <advitha@example.com>" -m "Add the late fee"
echo "Charged after the 10th" >> fees.txt
git commit --quiet -a --author="Advitha Anamaneni <advitha@example.com>" -m "Say when the late fee applies"
git switch --quiet main
git log --oneline --graph --all
Output
* 2131823 (feature/late-fee) Say when the late fee applies
* e78b165 Add the late fee
* 4988b51 (HEAD -> main) Add README and the term fee

main is simply behind feature/late-fee: everything on main is already in the feature branch. Merging it:

Terminal
git merge feature/late-fee
git log --oneline --graph
Output
Updating 4988b51..2131823
Fast-forward
 fees.txt | 2 ++
 1 file changed, 2 insertions(+)
* 2131823 (HEAD -> main, feature/late-fee) Say when the late fee applies
* e78b165 Add the late fee
* 4988b51 Add README and the term fee

Updating 4988b51.. and Fast-forward say what happened: Git moved main from its old commit to the feature's newest one. No merge commit was made, and the history is one straight line. Advitha's commits are now main's commits.

Fast-forward, --no-ff and --squash

Always Make a Merge Commit: --no-ff

A fast-forward leaves no sign that the work was done on a branch. Some teams want to see each feature as a group in the history. --no-ff (no fast-forward) makes a merge commit even when a fast-forward would be possible:

Terminal
git switch --quiet -c feature/hostel-fee
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
git switch --quiet main
git merge --no-ff --no-edit feature/hostel-fee
git log --oneline --graph
Output
Merge made by the 'ort' strategy.
 fees.txt | 1 +
 1 file changed, 1 insertion(+)
*   89417cd (HEAD -> main) Merge branch 'feature/hostel-fee'
|\
| * ea151c7 (feature/hostel-fee) Add the hostel fee
|/
* 2131823 (feature/late-fee) Say when the late fee applies
* e78b165 Add the late fee
* 4988b51 Add README and the term fee

The branch's commit sits on a side line that joins main in a merge commit. With many commits on the branch, the whole feature appears as one group, and git revert -m 1 <merge> can undo the whole feature in one step.

Only Fast-Forward: --ff-only

The opposite wish is a history without any merge commits. --ff-only merges only when a fast-forward is possible, and refuses otherwise. Here, main and the new branch both get a commit:

Terminal
git switch --quiet -c feature/exam-fee
echo 'Exam fee: $800' > exam.txt
git add exam.txt
git commit --quiet -m "Add the exam fee"
echo "Exam fee is paid once a year" >> exam.txt
git commit --quiet -am "Explain when the exam fee is paid"
git switch --quiet main
echo "Run the app." >> README.md
git commit --quiet -am "Explain how to run the app"
git merge --ff-only feature/exam-fee
Output
hint: Diverging branches can't be fast-forwarded, you need to either:
hint:
hint: 	git merge --no-ff
hint:
hint: or:
hint:
hint: 	git rebase
hint:
hint: Disable this message with "git config set advice.diverging false"
fatal: Not possible to fast-forward, aborting.

The branches have diverged, so a fast-forward is not possible, and Git stops without changing anything. You can then merge normally (accepting a merge commit), or rebase the feature onto main first so that a fast-forward becomes possible. Rebasing has its own lessons.

One Commit for the Whole Branch: --squash

--squash takes all the changes of a branch and stages them on the current branch as one set of changes, without committing and without a merge commit:

Terminal
git merge --squash feature/exam-fee
Output
Squash commit -- not updating HEAD
Automatic merge went well; stopped before committing as requested

Git says stopped before committing as requested. The changes are staged, but nothing is committed yet:

Terminal
git status -s
Output
A  exam.txt

exam.txt is staged as a new file, with the content of both of the branch's commits. Now you write the commit:

Terminal
git commit --quiet -m "Add the exam fee"
git log --oneline --graph
Output
* 44898c4 (HEAD -> main) Add the exam fee
* e295494 Explain how to run the app
*   89417cd Merge branch 'feature/hostel-fee'
|\
| * ea151c7 (feature/hostel-fee) Add the hostel fee
|/
* 2131823 (feature/late-fee) Say when the late fee applies
* e78b165 Add the late fee
* 4988b51 Add README and the term fee

The history of main is a straight line again, with one new commit containing both of the branch's commits. The two individual commits are not part of main.

That has one consequence: Git doesn't consider the branch merged, because its commits aren't in main's history:

Terminal
git branch -d feature/exam-fee
Output
error: the branch 'feature/exam-fee' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D feature/exam-fee'
hint: Disable this message with "git config set advice.forceDeleteBranch false"

After a squash merge, delete the branch with -D, once you have checked that the squash commit contains everything.

Syntax

CommandResult
git merge <branch>Fast-forward if possible, otherwise a merge commit
git merge --no-ff <branch>Always a merge commit
git merge --ff-only <branch>Fast-forward, or refuse
git merge --squash <branch>Stage all the changes, then you commit them as one commit

Which One Should You Use?

ChoiceHistory looks likeGood forWatch out for
Fast-forwardA straight lineSmall changes, solo work, branches already up to dateYou can't see where a feature started and ended
--no-ffA side line per feature, joined by a merge commitTeams that want to see features as groups, and undo them in one stepMany merge commits in a busy project
--squashA straight line, one commit per featureFeatures with messy work-in-progress commitsThe individual commits are lost from main, and the branch shows as unmerged

There is no single right answer. Most teams pick one style and set it in their hosting platform: GitHub, GitLab and Bitbucket all offer "Create a merge commit", "Squash and merge" and "Rebase and merge" (or "fast-forward") buttons for pull requests, and can allow only some of them.

Common Mistakes

Expecting a merge commit after every merge. If the current branch is only behind, Git fast-forwards. Use --no-ff if your team wants the merge commit.

Being surprised that --squash didn't commit. It stages the changes and stops. Run git commit to finish.

Deleting a squashed branch with -d. Git refuses, because the original commits aren't in main. Use -D after checking the squash commit.

Squashing a branch that you keep working on. Later squashes will include changes Git thinks it hasn't seen, which can cause conflicts. Squash a branch once, at the end, and start a new branch for more work.

Setting merge.ff false globally without telling anyone. Every merge then makes a merge commit, even when you just want to update a local branch. Prefer setting the style per project, or using the platform's settings.

Under the Hood

A fast-forward is possible when the current branch's commit is an ancestor of the other branch's commit: walking back from the other branch, you reach the current commit. Then the merge base is the current commit itself, and there is nothing to combine. Git just writes the other commit's ID into the branch file, exactly as a git reset would.

git merge-base --is-ancestor asks this question directly, and answers with its exit status, which echo $? prints (0 means yes, 1 means no):

Terminal
git merge-base --is-ancestor HEAD~1 HEAD; echo $?
git merge-base --is-ancestor main feature/exam-fee; echo $?
Output
0
1

The previous commit is an ancestor of the current one, so moving from it to HEAD would be a fast-forward. main is not an ancestor of feature/exam-fee, because main has commits the feature doesn't, which is why --ff-only refused.

Interview Corner

What is a fast-forward merge?

A merge where the current branch has no commits of its own since the other branch split from it. Git simply moves the current branch's pointer forward to the other branch's commit. No merge commit is created, and the history stays linear.

What does git merge --no-ff do, and why use it?

It always creates a merge commit, even when a fast-forward is possible. It keeps a record that the commits came from a feature branch, groups them visibly in the history, and lets you revert the whole feature with one git revert -m 1.

What is a squash merge?

git merge --squash applies all the changes of a branch to the current branch as staged changes, without a merge commit. You then commit them as one commit. It keeps main's history linear and tidy, but the branch's individual commits are not in main, and Git doesn't consider the branch merged.

When does git merge --ff-only fail?

When the branches have diverged, so the current branch is not an ancestor of the other. It then aborts without changing anything, and you can merge normally or rebase first.

Practice

Challenge: which kind of merge? In the same repository, you are on main. Create docs/readme, commit Built by Navika. to README.md there, and switch back to main. Then merge it with plain git merge and print the one-line log of the three newest commits.

What does Git print? Before you open the solution, decide whether this is a fast-forward or a merge commit.

Show solution
Terminal
git switch --quiet -c docs/readme
echo "Built by Navika." >> README.md
git commit --quiet -am "Credit the author"
git switch --quiet main
git merge docs/readme
git log --oneline -3
Output
Updating 44898c4..f670770
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
f670770 (HEAD -> main, docs/readme) Credit the author
44898c4 Add the exam fee
e295494 Explain how to run the app

A fast-forward. main got no new commits while docs/readme existed, so Git moved main to "Credit the author". No merge commit appears in the log.

Checkpoint. In a practice repository, merge three branches into main, one with each option: a plain fast-forward, --no-ff and --squash. Compare the three shapes in git log --oneline --graph.

Quick check: Fast-forward vs merge commit

4 questions
1When can Git fast-forward?
2Which option always creates a merge commit?
3After git merge --squash feature, what must you do?
4What does git merge --ff-only do when the branches have diverged?

Key Takeaways

Quick reference

CommandIt is used to
git merge <branch>Fast-forward if possible, otherwise make a merge commit
git merge --no-ff <branch>Always make a merge commit
git merge --ff-only <branch>Merge only by fast-forward
git merge --squash <branch>Stage a branch's changes for one commit
git config --global pull.ff onlyMake git pull refuse to create merge commits
git merge-base --is-ancestor <a> <b>Check whether a fast-forward from a to b is possible

Was this tutorial helpful?

Lesson 31 of 106