Non-Fast-Forward Merge (--no-ff Option)
Even when the status of branches is the same as the fast-forward case, you may want to create a new commit. If that is the case, you can use the --no-ff
option.
The command enforces the creation of the commit shown in the main figure. The merge result becomes almost the same as the case on the previous page; the only difference is that the previous case has commit M4 before the HEAD on the master branch.
We'll explain how the non-fast-forward merge with the --no-ff
option works in more detail with command line examples below.
Command Line Example
The command line image below is a demonstration of the commit and merge actions, which are the same as the upper illustration in the main figure. M1, M2, M3, A1, and A2 are the commit messages which were already made before. We'll explain the commands and responses in the command line in three steps.
- Confirm the pre-merge commit history status by running the
git log
command on the master branch. You can see that the commit histories and branch statuses are the same as the ones illustrated in the main figure. (In this status, commit M4 has already been erased from the last status on the previous page to demonstrate the non-fast-forward merge with the--no-ff
option. Check the practice section below to see how to come to this status.) - Run the
git merge
command with the--no-ff
option. When running the command, you'll see a temporary message on the command line saying "hint: Waiting for your editor to close the file ..." and a text editor is launched as shown in the image below. A commit message is already written there. If you want, you can modify the commit message. If you are fine with the message, save and close the file. - Check the branch status by running the
git log
command. You can see that the HEAD of master is ahead of HEAD of Branch_A as we created a new commit for the merge operation.
Example: commit message in a text editor
Merge branch 'Branch_A'
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
(The Windows version of VS Code may not open the text editor. It may automatically create and save the commit message.)
For a better understanding, please go through the following practice section.
Practice
Developer A (Project Owner Role)
Objective:
Check how the Non-Fast-Forward merge with the --no-ff option works
1. Prepare a practice file
In the practice on the previous page, we created commit M4 on the master branch and merged Branch_A with the master branch. For this practice purpose, we need to reverse the commit history before Branch_A is merged.
To reverse the HEAD of the master branch to commit M3, reset the merge operation by running the git reset --hard
command on the master branch. Use the commit hash of commit M3 which is generated on your computer. Also, make sure to use the --hard
option. If you don't use the option, the statuses of the Working Tree, INDEX, and HEAD will be mixed.
git checkout master
git reset --hard e12beda
git log --oneline
You'll see that the HEAD of the master branch is back to Commit M3, which is aligned with the upper illustration on the main figure for the master branch.
e12beda (HEAD -> master) M3
f5ff7aa M2
8bf5c03 M1
2. Perform the merge commit
Now you are ready to execute the merge command. As you are already on the master branch, run the following command with the --no-ff
option.
git merge --no-ff Branch_A
As shown on the previous page, the command line goes into interactive mode while a text editor is launched. The text editor is used to write a commit message. A simple message is already written. Unless you want to modify the message, you can close the text editor as it is.
Auto-merging git_branch_practice.html
hint: Waiting for your editor to close the file...
Merge branch 'Branch_A'
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
Once you close the editor, the merge command is completed like below.
Merge made by the 'ort' strategy.
git_branch_practice.html | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
3. Check commit histories and branch status
Check the status of the master branch first. Add the "--graph" option this time as the branch status is slightly more complicated than the one in the previous example.
git log --oneline --graph
You can see that the HEAD of the master branch is ahead of the HEAD of Branch_A. Also, you can confirm that a new commit is created by this operation. The only difference from the previous case (the Fast-forward no-option case) is that there is no M4 commit and commit hash for the last commit.
* 73c7957 (HEAD -> master) Merge branch 'Branch_A'
|\
| * a4c4eb0 (Branch_A) A2
| * 62dda63 A1
|/
* e12beda M3
* f5ff7aa M2
* 8bf5c03 M1
Next, check the status of Branch_A.
git checkout Branch_A
git log --oneline
You can see that nothing has changed for Branch_A
a4c4eb0 (HEAD -> Branch_A) A2
62dda63 A1
e12beda M3
f5ff7aa M2
8bf5c03 M1