Git Fast-Forward Merge: Advance a Branch
What is a fast-forward merge?
If the current branch tip is an ancestor of the branch being merged, Git can advance the current branch to the existing later commit. This is a fast-forward: no new merge commit is needed. A merge affects the currently checked-out branch, not the named source branch. Start in the fresh ff lab described in the chapter guide.
git switch -c featureprintf "Milk\n" > feature.txtgit add -- feature.txtgit commit -m "Add feature"git switch maingit merge --ff-only featureBefore: main → A, feature → B, B has parent A. After: both names point to B. The working tree includes feature.txt.
Inspect the result
Check the history and both reference targets instead of assuming a new commit was created. HEAD remains attached to main.
git log --oneline --graph --allgit rev-parse main featuregit status --shortThe two IDs match, the graph has two commits, and short status is empty. IDs vary across environments.
When fast-forward is impossible
If both branches have unique commits, moving main directly to feature would leave main’s unique history out of the result. --ff-only refuses this integration. Use the separate diverged lab from the guide; do not run it after the first example.
The first command fails; in this clean fixture no merge is in progress and the original tip is unchanged. Choose a three-way merge if both histories should be integrated.
Practice
Predict the result if feature already points to main. Then explain why --ff-only is a useful safeguard in a linear-history workflow.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Fast-Forward Merge: Advance a Branch — knowledge check
Reason about merge history, conflict state and safe integration.
main is at A; feature is at B whose parent is A. What does --ff-only do?
Which branch is changed by merging feature while main is checked out?
Both branches have unique commits. What does --ff-only do?
After a fast-forward, how many new commits did the merge itself create?
How can you check whether main and feature now identify the same commit?