How can I tell whether a Git merge will fast-forward or create a merge commit?
Quick answer
A merge can fast-forward when the current branch tip is an ancestor of the branch being merged: Git moves the current branch pointer forward without adding a merge commit. If both branches have commits the other does not, their histories have diverged and a true merge is needed, recorded in a merge commit. Compare the commit ranges before merging; they show history relationships, not whether conflicts will occur. [S1]
The question, the details, the next step.
A practical checklist
Confirm the current branch with
git branch --show-current.Replace
topicwith the exact branch you plan to merge.Inspect both
git log --oneline HEAD..topicandgit log --oneline topic..HEAD.Use
git merge-base HEAD topicto help identify the shared ancestor.Treat ancestry checks as history information, not a prediction of merge conflicts.
Use
--no-ffif you want a merge commit even when a fast-forward is possible.
A few more useful answers
If `git log topic..HEAD` shows commits, can the merge still fast-forward?
If that range shows commits unique to the current branch while topic also has commits beyond the shared history, the histories have diverged, so a true merge is needed. [S1]
Does a fast-forward check tell me whether there will be conflicts?
No. Ancestry and commit-range checks describe the relationship between branch tips; they do not guarantee a conflict-free merge. [S1]
Can I choose to create a merge commit even when Git could fast-forward?
Yes. Git documents --no-ff as a way to suppress a fast-forward and record a merge commit instead. [S1]
Sources
References used for this guide.
- [S1] Git git-merge documentation: fast-forward and true merges git-scm.com · Checked
