Python for Network Engineers

Difference Between Fast Forward and Recursive Merge

by: George El., February 2019, Reading time: 3 minutes

In this post I will clarify the difference between a fast-forward and a three-way or recursive merge and why you must use no fast-forward commmit in projects shared by many people.

As you can see in the following graph I have two branches: branch master has 3 commits which correspond to changes 1,2,3 and branch branch1 has 3 commits which correspond to changes 4,5,6

$  git log --graph --decorate --oneline --all
* 0af205d (branch1) added 6
* ea4c604 added 5
* 82b3453 added 4
* ba9f6ed (HEAD -> master) added 3
* 5297b54 added 2
* adfa222 added 1

From the master branch I merge branch1

$ git merge branch1
Updating ba9f6ed..0af205d
Fast-forward
 4.txt | 0
 5.txt | 0
 6.txt | 0
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 4.txt
 create mode 100644 5.txt
 create mode 100644 6.txt

As you see there is a message “Fast-forward”. This means that it just moves the head forward to branch1. this is possible because the master branch had no commits after we created branch1.

$  git log --graph --decorate --oneline --all
* 0af205d (HEAD -> master, branch1) added 6
* ea4c604 added 5
* 82b3453 added 4
* ba9f6ed added 3
* 5297b54 added 2
* adfa222 added 1

Lets go back how it was by doing a hard reset

$  git reset --hard ba9f6ed
HEAD is now at ba9f6ed added 3
$  git log --graph --decorate --oneline --all
* 0af205d (branch1) added 6
* ea4c604 added 5
* 82b3453 added 4
* ba9f6ed (HEAD -> master) added 3
* 5297b54 added 2
* adfa222 added 1

Now we specify that we dont want fast forward merge with the flag “–no-ff”

$ git merge --no-ff branch1
Merge made by the 'recursive' strategy.
 4.txt | 0
 5.txt | 0
 6.txt | 0
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 4.txt
 create mode 100644 5.txt
 create mode 100644 6.txt

Now lets look again at the graph

$  git log --graph --decorate --oneline --all
*   8bc610f (HEAD -> master) Merge branch 'branch1'
|\
| * 0af205d (branch1) added 6
| * ea4c604 added 5
| * 82b3453 added 4
|/
* ba9f6ed added 3
* 5297b54 added 2
* adfa222 added 1

You see now that it created a new commit 8bc610f that has the changes from the other two branches.

When you are working in a team and you work on a feature branch, you don’t want all the individual changes to be merged on the master branch. You need to keep the master branch as clean as possible. In these cases you have to use the “–no-ff” to avoid implicit fast forward commits

comments powered by Disqus