brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git merge --no-ff keeps a merge commit even when Git could fast-forward

Git
~/tips/tip-git-merge-no-ff

git merge --no-ff feature -m "Merge feature"

Always creates a merge commit, so a finished feature branch stays visible as a group in the history.

If nothing new landed on main while you worked on a branch, git merge just moves the main pointer forward. The history ends up as one straight line and nobody can tell afterwards that those commits were once a feature of their own. One flag changes that.

git switch main
git merge --no-ff feature -m "Merge feature"

The documentation says that with --no-ff Git creates a merge commit in all cases, even when the merge could instead be resolved as a fast-forward. In a throwaway repository, where the branch ff1 has one commit on top of main, a plain merge only moves the pointer. With the flag, git log --oneline --graph shows a merge commit with the branch commit hanging off it:

*   7d8df18 Merge ff1
|\
| * 2cd994f x
|/
* 94d110f Add feature (squashed)

The payoff comes later: you can see where the feature started and ended, and the merge commit is one place to point at when someone asks when it landed.

If you want this every time, there is a setting for it: git config merge.ff false. The docs describe it as the same as giving --no-ff on the command line.

One caveat: when the branch is a real divergence, Git makes a merge commit anyway, so the flag changes nothing there. It only matters for the fast-forward case. And the extra commit is the whole point, so if you like a perfectly linear history, leave the flag out and rebase before you merge instead.

Further reading: git-merge documentation

More Git tips git show-branch main feature git log --stat -5 git merge --squash feature git merge --abort git branch --edit-description [<branch-name>] All Git tips (144) All topics

Link