git,

Git merge: --ff vs --no-ff

Oct 27, 2022 · 2 mins read · Post a comment

Besides Git merge being a common operation when merging one branch into another, there are these two popular flags bamboozling developers: --ff or so-called fast-forward, and --no-ff meaning no fast-forward. It comes down to whether to create a merge commit or not.

Prerequisites

  • Git

Solution

--ff

By default, merging is done including the --ff parameter. What it means is, it does not create a merge commit, instead updates the branch pointer (HEAD) to point to the tip of the merged branch (the latest commit).

A common example would be when running the git pull command in order to update the branch (pull changes). Under the hood git pull = git fetch + git merge.

git checkout develop
git pull develop

or, the equivalent:

git checkout develop
git fetch --prune && git merge develop --ff

Note: Excluding the --ff leads to the same behavior as it’s the default one.

However, that’s not always the case. The merge command will try to create a merge commit only when the merged branch is not a descendant of the current Git history meaning the merged branch commit(s) are not children, or great-children, or great-great-children, etc. of the current commit. This is where --no-ff comes into play.

--no-ff

Forces to always create a merge commit. For instance merging a feature or develop branch into main, or even merging a GitHub pull request would be the same thing.

Example merging feature-abc branch into develop.

git checkout develop
git fetch --prune && git pull develop
git merge feature-abc --no-ff

--ff-only

Lastly, --ff-only will force not to create a merge commit although when it’s not possible, the merge will be cancelled and exits with a non-zero code.

Conclusion

To find more neat Git commands and hacks, simply browse the Git category. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.

git