DEV Community

Cover image for Day 8/30 - Git Merge --no-ff – Force a merge commit (avoid fast-forward)
Ruqaiya Beguwala
Ruqaiya Beguwala

Posted on • Originally published at Medium

Day 8/30 - Git Merge --no-ff – Force a merge commit (avoid fast-forward)

What is Git Merge?

Git merge is the command used to combine changes from different branches in your repository. When you merge, Git integrates the changes from one branch (the "source") into another branch (the "target").

Example

You're on main branch and want to merge feature branch:

git checkout main
git merge feature
Enter fullscreen mode Exit fullscreen mode

Git performs the merge in one of two ways:

1. Fast-forward merge:

When possible (if the target branch hasn't diverged), Git simply moves the branch pointer forward.
Before:

A---B---C (main)
        \
         D---E (feature)
Enter fullscreen mode Exit fullscreen mode

After git merge feature:

A---B---C---D---E (main, feature)
Enter fullscreen mode Exit fullscreen mode

2. Three-way merge (--no-ff):

When branches have diverged, Git creates a new "merge commit" that combines the changes.
Before:

A---B---C---F (main)
      \
       D---E (feature)
Enter fullscreen mode Exit fullscreen mode

After git merge feature:

A---B---C---F---M (main)
      \       /
       D-----E (feature)
Enter fullscreen mode Exit fullscreen mode

What is --no-ff?

When you merge branches in Git, by default it will do a "fast-forward" merge if possible (when the target branch hasn't diverged). --no-ffforces Git to create a new merge commit even when a fast-forward would be possible.

Why Use --no-ff?

  • Better history visualization: You can clearly see when features were merged
  • Easier to revert: You can revert the entire merge with one commit
  • Logical grouping: Groups all feature work together in history

Advanced Practical Use Case: Release Management

Imagine you're working on a project with:

  • main branch for production code
  • develop branch for ongoing development
  • Feature branches for individual features
# Create develop branch
git checkout -b develop main

# Work on feature1
git checkout -b feature1 develop
# ... make several commits ...
git checkout develop
git merge --no-ff feature1 -m "Merge feature1 for release 1.2"

# Work on feature2
git checkout -b feature2 develop
# ... make several commits ...
git checkout develop
git merge --no-ff feature2 -m "Merge feature2 for release 1.2"

# When ready to release
git checkout main
git merge --no-ff develop -m "Release 1.2"
Enter fullscreen mode Exit fullscreen mode

This creates a clear history showing:

  • Exactly what features went into each release
  • When features were merged into development
  • The complete context of each release

Tips and Tricks

1. Always use a merge message:

git merge --no-ff feature -m "Meaningful merge message"
Enter fullscreen mode Exit fullscreen mode

2. Combine with --no-commit to review before finalizing:

git merge --no-ff --no-commit feature

Review changes
git commit -m "Merge feature after review"
Enter fullscreen mode Exit fullscreen mode

3. Alias for convenience:

git config --global alias.mnf 'merge --no-ff'
# Then use: git mnf feature
Enter fullscreen mode Exit fullscreen mode

4. View merge differences:

git diff HEAD^1 HEAD^2  # Shows differences between merged branches
Enter fullscreen mode Exit fullscreen mode

5. When NOT to use --no-ff:

  • For small, trivial changes
  • When you want a linear history for simple branches
  • Clean up merged branches: bash
git branch -d feature  # Deletes the merged branch
Enter fullscreen mode Exit fullscreen mode

Other Merge Options

1. --ff-only: Only allow fast-forward merges (fails if not possible)

git merge --ff-only feature
Enter fullscreen mode Exit fullscreen mode

2. --squash: Combine all feature commits into one new commit

git merge --squash feature
git commit -m "Squashed feature changes"
Enter fullscreen mode Exit fullscreen mode

3. --abort: Cancel a merge in progress (when conflicts occur)

git merge --abort
Enter fullscreen mode Exit fullscreen mode

4. --no-commit: Perform merge but don't auto-commit (lets you review)

git merge --no-commit feature
Enter fullscreen mode Exit fullscreen mode

5. -m: Add custom merge message

git merge -m "Custom merge message" feature
Enter fullscreen mode Exit fullscreen mode

6. --strategy: Specify merge strategy (ours, theirs, etc.)

git merge -s ours feature  # Keep our version in conflicts
Enter fullscreen mode Exit fullscreen mode

Conclusion

Git’s merge --no-ff is a powerful tool for maintaining a clean, readable project history—especially in team environments. By forcing Git to create a dedicated merge commit, you preserve the context of feature branches, making it easier to track changes, revert merges, and understand project evolution.

Want to dive deeper? Check out Git’s official docs on merge strategies or explore advanced workflows like Git Flow.


Up next: git merge --squash – Combine multiple commits into one.


Daily advance GIT tips in your inbox—worth starting? Respond to my poll here🚀
For more useful and innovative tips and tricks, Let's connect on Medium

Top comments (0)