What is a fast-forward merge and when does it happen?
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.
Previous
How does Git store objects internally?
Next
What is the difference between origin/main and main in Git?