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
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)
After git merge feature:
A---B---C---D---E (main, feature)
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)
After git merge feature:
A---B---C---F---M (main)
\ /
D-----E (feature)
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-ff
forces 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"
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"
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"
3. Alias for convenience:
git config --global alias.mnf 'merge --no-ff'
# Then use: git mnf feature
4. View merge differences:
git diff HEAD^1 HEAD^2 # Shows differences between merged branches
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
Other Merge Options
1. --ff-only
: Only allow fast-forward merges (fails if not possible)
git merge --ff-only feature
2. --squash
: Combine all feature commits into one new commit
git merge --squash feature
git commit -m "Squashed feature changes"
3. --abort
: Cancel a merge in progress (when conflicts occur)
git merge --abort
4. --no-commit
: Perform merge but don't auto-commit (lets you review)
git merge --no-commit feature
5. -m
: Add custom merge message
git merge -m "Custom merge message" feature
6. --strategy
: Specify merge strategy (ours, theirs, etc.)
git merge -s ours feature # Keep our version in conflicts
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)