How to force a merge commit (Git)

Reginald LynchOctober 31st 2020, 5:44

Git supports almost any workflow. We have often encountered a situation that requires a merge commit while merging a feature, even though it can be done with a fast-forward merge. Those who requested it often use it to indicate that you have actually merged in a feature and want to store the information in the repository.

Step 1 : Start by checking out a local branch remoteOldbugFix that tracks origin/stable-3.1:

git clone https://git.eclipse.org/r/jgit/jgit 
cd jgit 
git checkout -b remoteOldBugFix --track origin/stable-3.1

Step 2 : To force a merge commit, you need to use the --no-ff flag; no-ff means no fast forward. We will also use the --quiet flag to minimize the output and --edit to allow us to edit the commit message. Unless you have a merge conflict, Git will create the merge commit for you automatically:

git merge origin/stable-3.2 --no-ff --edit --quiet 

Step 3 : The commit message editor will open, and you can write a commit message. Closing the editor creates the merge commit and we are done.

Step 4 : To verify this, you can reset back to origin/stable-3.1 and perform the merge without the --no-ff flag:

git reset --hard  remotes/origin/stable-3.1 

Step 5 : Now, perform the merge with the following command:

git merge origin/stable-3.2 --quiet

Step 6 : You can see the difference using Gitk. The following screenshot shows the fast- forward merge; as you can see, our remoteOldBugFix branch points to origin/stable-3.2: