289

Git merge allow us to perform fast forward and no fast fast forward branch merging. Any ideas when to use fast forward merge and when to use no fast forward merge?

Chau Chee Yang
  • 15,982
  • 13
  • 62
  • 126

5 Answers5

341

The --no-ff option is useful when you want to have a clear notion of your feature branch. So even if in the meantime no commits were made, FF is possible - you still want sometimes to have each commit in the mainline correspond to one feature. So you treat a feature branch with a bunch of commits as a single unit, and merge them as a single unit. It is clear from your history when you do feature branch merging with --no-ff.

If you do not care about such thing - you could probably get away with FF whenever it is possible. Thus you will have more svn-like feeling of workflow.

For example, the author of this article thinks that --no-ff option should be default and his reasoning is close to that I outlined above:

Consider the situation where a series of minor commits on the "feature" branch collectively make up one new feature: If you just do "git merge feature_branch" without --no-ff, "it is impossible to see from the Git history which of the commit objects together have implemented a feature—you would have to manually read all the log messages. Reverting a whole feature (i.e. a group of commits), is a true headache [if --no-ff is not used], whereas it is easily done if the --no-ff flag was used [because it's just one commit]."

Graphic showing how --no-ff groups together all commits from feature branch into one commit on master branch

Mark
  • 907
  • 14
  • 24
Ivan Danilov
  • 13,026
  • 5
  • 43
  • 64
  • 4
    And fast-forwards are great for when you've got a collection of closely-related branches that now and then you want to just move together. Not all merges are real history events. – Cascabel Jul 15 '11 at 00:44
  • 3
    Why maintaining several branches then? If they're close-related - why not to do everything on single branch? – Ivan Danilov Jul 15 '11 at 01:00
  • 11
    Closely-related doesn't mean identical. Besides, workflow isn't always neat. You don't always make the commits you think you're going to, you don't always branch from the best place. Maybe you start a couple features from one place, start working on one of them, realize it's generic, and fast-forward the other one to it before diverging. – Cascabel Jul 15 '11 at 01:02
  • Another common thing is for integration to have taken place on a branch besides master. All the merges you care about have already taken place; you're just making sure it's okay without touching master. Then you merge it into master - and that can be a fast-forward. – Cascabel Jul 15 '11 at 01:05
  • 2
    As for first reply, my understanding is that OP wants to know best practices to follow. Things happen and not everything is ideal but this seems more like some forced compromise. – Ivan Danilov Jul 15 '11 at 01:12
  • And about the second I have to agree. There's many possible usages for `--ff` and `--no-ff` the question is only what kind of history you want to achieve. Technically git is advanced enough to satisfy almost any demand :) – Ivan Danilov Jul 15 '11 at 01:15
  • In a team with several developers working on various branches I would consider keeping the history of individual commits critical. svn is all but abandoned these days, but there are some companies who insist on using it because "we've always used it" – jcpennypincher Oct 21 '14 at 20:37
  • 2
    It's worth noting that the benefits of `--no-ff` on your commit history may not be immediately evident when using basic tools like `git log`, which will continue to show all commits from all branches that have been merged into your current branch. That said, the benefits become clearer when using e.g. `git log --first-parent` on an integration branch such as `develop` or `master`. If you religiously use `--no-ff` then that will _exclusively_ display merge requests, while `git log` will still provide a (more) comprehensive history. That's why Vincent recommends it for use with **GitFlow**. – Jeremy Caney Dec 28 '19 at 01:43
17

I can give an example commonly seen in project.

Here, option --no-ff (i.e. true merge) creates a new commit with multiple parents, and provides a better history tracking. Otherwise, --ff (i.e. fast-forward merge) is by default.

$ git checkout master
$ git checkout -b newFeature
$ ...
$ git commit -m 'work from day 1'
$ ...
$ git commit -m 'work from day 2'
$ ...
$ git commit -m 'finish the feature'
$ git checkout master
$ git merge --no-ff newFeature -m 'add new feature'
$ git log
// something like below
commit 'add new feature'         // => commit created at merge with proper message
commit 'finish the feature'
commit 'work from day 2'
commit 'work from day 1'
$ gitk                           // => see details with graph

$ git checkout -b anotherFeature        // => create a new branch (*)
$ ...
$ git commit -m 'work from day 3'
$ ...
$ git commit -m 'work from day 4'
$ ...
$ git commit -m 'finish another feature'
$ git checkout master
$ git merge anotherFeature       // --ff is by default, message will be ignored
$ git log
// something like below
commit 'work from day 4'
commit 'work from day 3'
commit 'add new feature'
commit 'finish the feature'
commit ...
$ gitk                           // => see details with graph

(*) Note that here if the newFeature branch is re-used, instead of creating a new branch, git will have to do a --no-ff merge anyway. This means fast forward merge is not always eligible.

themefield
  • 2,454
  • 25
  • 28
8

When we work on development environment and merge our code to staging/production branch then Git no fast forward can be a better option. Usually when we work in development branch for a single feature we tend to have multiple commits. Tracking changes with multiple commits can be inconvenient later on. If we merge with staging/production branch using Git no fast forward then it will have only 1 commit. Now anytime we want to revert the feature, just revert that commit. Life is easy.

Pritom Nandy
  • 1,185
  • 9
  • 5
0

It is possible also that one may want to have personalized feature branches where code is just placed at the end of day. That permits to track development in finer detail.

I would not want to pollute master development with non-working code, thus doing --no-ff may just be what one is looking for.

As a side note, it may not be necessary to commit working code on a personalized branch, since history can be rewritten git rebase -i and forced on the server as long as nobody else is working on that same branch.

g24l
  • 2,819
  • 11
  • 27
0

Git visualization

Here are the git log visualizations with the differences. These are what the trees looked like creating three branches from dev called 1 2 3, then merging with and without fast-forward. I'll put the setup code at the bottom. You can paste a paragraph of commands into your terminal to quickly setup and reset different git scenarios, which was very helpful in learning git.

Notice that with fast-forward, git doesn't even indicate a merge.

--no-ff         --ff (default)

* Merge 3
| * 3
* | Merge 2     * Merge 3
| | * 2         | * 3
| |/            * | Merge 2
* / Merge 1     | | * 2
|/              | |/
| * 1           * / 1
|/              |/
* main          * main

It's worth comparing this approach with rebasing.

Setup/teardown

You can run this repeatedly and it'll delete and reinitialize the repo. It's for windows, so I think you'll just have to change the filepath and the rd remove directory commands if you're on *nix.

To see the behavior with fast-forward, remove --no-ff from the merge commands at the end. Remove the --pretty piece if you want to see the commit IDs.

cd \user\docs\code\learning\github\sandbox
rd /s /q 0.git 1 2 3
git init --bare 0.git
git clone 0.git 1
cd 1
git config user.name "user"
git config user.email "user@email.com"
git commit --allow-empty -m main
git switch -c 1 main
git commit --allow-empty -m 1
git switch -c 2 main
git commit --allow-empty -m 2
git switch -c 3 main
git commit --allow-empty -m 3
git switch main
git merge --no-ff 1
git merge --no-ff 2
git merge --no-ff 3
git log --graph --oneline --first-parent --all --pretty=%s
JDG
  • 287
  • 1
  • 10