How to disable fast forward merges? What are its pros and cons in Git?

GitGitHubOpen SourceSoftware & Coding

Fast forward merges can be disabled −

  • At the time of merging
  • For the entire repository
  • For all repositories.

Disabling fast forward merges has both pros and cons.

When we disable fast forward merge, git will perform a merge commit to merge the changes from both the branches. The drawback of merge commit is that it becomes hard to read and understand the commit history, especially when we have many branches. If your team prefers to keep a linear history of branches, then one should go for fast forward merge. Disabling fast forward merge will create merge commits, which pollutes the commit history.

The benefits of disabling fast forward merge (enabling merge commits) are that it shows the true reflection of the commit history. Merge Commits will show when the branches were merged and how they were merged. Another advantage is that Merge Commits helps to undo a feature easily. Undoing a feature is complex if we are to maintain a linear history.

Using the below command, we can disable fast forward merge at the time of merging.

$git merge --no-ff branch_name

We can disable fast forward merge for a particular repository or for all our repositories.

git config ff no

For all repositories use the --global flag

git config --global ff no

Example

Let us take an example to see how to disable fast forward merge using --no-ff option. In this example we are creating a text file - file1.txt, adding some content, staging and lastly committing the file. Let’s call it as commit c1 as shown in the diagram below. We are creating a new branch feature which points to the same commit c1.

Now we will move the feature branch ahead by appending new values to file.txt and committing the changes. Let us call the commit as c2, as shown in the diagram.

Now we need to merge changes from feature branch to master using the following command

$git merge --no-ff feature

This will create a new merge commit and the Master branch will now point to the Merge Commit as shown in the diagram.

Example

$ git init
$ echo abc>file1.txt
$ git add .
$ git commit -m 'fiel1.txt'
$ git branch feature
$ git switch feature
$ echo pqr>>file1.txt
$ git add .
$ git commit -m 'pqr'
$ git switch master
$ git merge --no-ff feature
$ git log --all --oneline --graph

Output

Merge made by the 'recursive' strategy.
file1.txt | 1 +
1 file changed, 1 insertion(+)

* 62e51ff (HEAD -> master) Merge branch 'feature'
|\
| * c94629d (feature) pqr
|/
* 35d3b1b fiel1.txt
raja
Published on 28-Apr-2021 13:07:34
Advertisements