What is a fast-forward merge and when does it happen?
Why Interviewers Ask This
Advanced questions like this reveal whether a candidate has internalized Git & GitHub deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.
Answer
A fast-forward merge occurs when the target branch has not diverged from the source branch — the target branch pointer is an ancestor of the source branch. In this case, Git simply moves the target branch pointer forward to the latest commit of the source branch. No merge commit is created — the result is a perfectly linear history. When does it happen: you create a feature branch from main, add commits to the feature, and main has no new commits in the meantime. Merging: git checkout main; git merge feature — since main's HEAD is a direct ancestor of feature's HEAD, Git fast-forwards. Prevent fast-forward: git merge --no-ff feature — always creates a merge commit even when fast-forward is possible. This is useful when you want the merge commit to document that feature branches were used (common in GitFlow). Enforce fast-forward only: git merge --ff-only feature — fails with an error if fast-forward isn't possible (the branches have diverged). Used in automation to prevent accidental merge commits. GitHub "Rebase and merge" option performs a fast-forward by rebasing the feature onto main first, then fast-forward merging. The distinction matters for audit trails and understanding project history.
Common Mistake
A common mistake is memorizing definitions without understanding implications. When asked this question, go one level deeper — explain what happens when this concept is misused or ignored.
Previous
How does Git store objects internally?
Next
What is the difference between origin/main and main in Git?