How to Merge Branches Without Fast Forward in Git

Azhar Bashir Khan Feb 02, 2024
How to Merge Branches Without Fast Forward in Git

This tutorial will see about merging branches without fast forward in Git.

We use Git to work in a collaborative development environment. We create many branches for different purposes, and eventually, we merge those branches into the main branch to provide a release build.

We merge branches in Git with fast forward or without fast forward.

We will now illustrate this with an example.

Using the git merge Command With and Without --no-ff (No Fast Forward) Option in Git

In a collaborative development environment, we often would create multiple branches in Git, to serve different purposes. We eventually then merge those branches into the main branch to provide a release build.

Sometimes, our mainline branch's HEAD commit is an ancestor of the commit of the branch we are trying to merge. In such cases, the merge is done with fast forward.

A fast forward merge can occur when a linear path from the current branch tip to the target branch. Suppose we have branch feature1 and we are merging it into main, our mainline branch.

Now, suppose the HEAD commit of the main branch is an ancestor of the commit of the branch feature1, the one we want to merge.

In such cases, instead of merging the branches, all Git has to do to integrate the histories is move (i.e.) fast forwards the current branch tip up to the target branch tip.

Thus, in our case, with fast forward, the main branch’s current HEAD will be moved up to the feature1 branch tip.

Please see below an illustration of the situation of the branches, main and feature1, before the merge.

              E---F---G feature1
             /
A---B---C---D main

Thus, a fast forward will happen when we merge the two branches using the git merge command.

To do a merge, we execute the command as follows.

$ git merge feature1

After executing the command, the main branch will be fast forwarded.

Please see the illustration of the fast forward below.

              E---F---G feature1, main
             /
A---B---C---D

Thus, now the main branch’s current HEAD is now fast forwarded. No merge commit is created in this case.

Sometimes, we may want to do an actual merge instead of a fast forward. Let’s say we want to maintain the branch topology.

In such a case, we can use the git merge command with option --no-ff.

The command option --no-ff causes to create a merge commit in all cases, even when the merge could instead be resolved as a fast forward.

Thus, in our case, to do a merge without fast forward, we need to do as follows.

$ git merge --no-ff feature1

Thus, now the git merge command merges the branch feaure1 into the main branch and generates a merge commit (even if it was a fast forward merge).

This is useful for documenting all merges that occur in your repository.

Thus, we have elaborated on how to merge the branches without fast forward in Git.

For more information, please visit -

  1. Git Merge
  2. git-merge

Related Article - Git Merge