In this article, we will see what does --no-ff option does in git along with examples.

When we merge two branches, by default Git uses the fast-forward merge method, which simply moves the branch pointer to the latest commit without creating a merge commit.

However, sometimes it is necessary to preserve the branch history and create a merge commit even if it is not necessary. In such a case, we have to use –no-ff option.

With this option, Git will preserve the branch history and create a merge commit that shows the point at which the branches diverged and were later merged.

This option is useful when we want to preserve the history of our branch & to maintain the clear record of when and where merges occurred.

Let us see some examples of how –no-ff work?

1. Merging a branch with main branch

Assume, we have a branch which has a changes specific to feature that we are working on. After completing the work, we want to merge these changes to the main branch.

If we don’t use the --no-ff option here, git simply moves the branch pointer to the latest commit. So, to preserve the commit history we have to use --no-ff option.

git checkout main
git merge --no-ff feature-branch

After entering the merge command along with –no-ff, you will see a pop up terminal which ask to enter a commit message for the merge commit.

2. Reviewing the changes before merge

The command that we have see in the previous example, creates a merge commit and merge the changes from feature branch as part of that commit.

Suppose if we want to review the changes that will be merged before creating a merge commit. In this case, we can use the --no-commit option along with the --no-ff option.

Following is the example:

git merge --no-commit --no-ff feature-branch

The above command merges the changes from the feature branch but does not commit them. Instead, it leaves the changes in the staging area, allowing us to review them before creating a merge commit.

We can then review the changes and modify them if necessary before committing them.

Categorized in:

Tagged in: