0
0
GitHow-ToBeginner · 3 min read

How to Use No-FF Merge in Git: Syntax and Example

Use git merge --no-ff <branch-name> to merge a branch without fast-forwarding. This creates a merge commit even if Git could fast-forward, preserving the branch history clearly.
📐

Syntax

The git merge --no-ff <branch-name> command merges the specified branch into the current branch without fast-forwarding. This means Git will always create a new merge commit, even if the merge could be done by simply moving the branch pointer.

  • git merge: The command to merge branches.
  • --no-ff: Option to disable fast-forward merges.
  • <branch-name>: The branch you want to merge into your current branch.
bash
git merge --no-ff <branch-name>
💻

Example

This example shows how to merge a feature branch into the main branch using --no-ff. It keeps the merge commit to show the feature branch's work as a separate unit.

bash
git checkout main
# Make sure main is up to date
git pull origin main

# Merge feature branch with no fast-forward
git merge --no-ff feature-branch

# Push changes to remote
git push origin main
Output
Merge made by the 'recursive' strategy. feature-file.txt | 1 + 1 file changed, 1 insertion(+)
⚠️

Common Pitfalls

One common mistake is forgetting to use --no-ff and ending up with a fast-forward merge, which loses the branch history. Another is using --no-ff unnecessarily on trivial merges, cluttering history with extra commits.

Always check if preserving the branch history is important before using --no-ff.

bash
git merge feature-branch  # This may fast-forward and skip merge commit

git merge --no-ff feature-branch  # This forces a merge commit
📊

Quick Reference

CommandDescription
git merge --no-ff Merge branch with a merge commit, no fast-forward
git merge Merge branch, may fast-forward if possible
git checkout Switch to the specified branch
git push origin Push local branch changes to remote

Key Takeaways

Use git merge --no-ff <branch> to keep a merge commit and preserve branch history.
Without --no-ff, Git may fast-forward and lose the separate branch record.
Use --no-ff when you want clear history of feature branches or work units.
Avoid unnecessary --no-ff merges to keep history clean.
Always pull latest changes before merging to avoid conflicts.