Non-Fast-Forward Merge (No Option)
As explained, when the parent branch (e.g., master branch) is already ahead of the diverged point of the child branch, the merge action becomes a non-fast-forward merge. In this case, the merge action is recorded as a new commit.
The illustration in the main figure describes the before and after of the non-fast forward merge. In this case, the HEAD of the master branch moves forward integrating all commit histories under Branch_A.
We'll explain the non-fast-forward merge 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 already made before this demonstration. 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, the merge operation done on the previous page has been reversed, and commit M4 has already been added to demonstrate the non-fast-forward merge. Check the practice section below to see how to come to this status.) - Run the
git merge
command. 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 the master branch is ahead of the 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 (no option) works
1. Prepare a practice file
In the practice on the previous page, we already made commits M1, M2, and M3 on the master branch and A1 and A2 on Branch_A. Also, Branch_A was merged into the master branch. For this practice purpose, we need to reverse the commit history before Branch_A was 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.
Switched to branch 'master'
HEAD is now at 3ad2537 M3
e12beda (HEAD -> master) M3
f5ff7aa M2
8bf5c03 M1
Next, create commit M4 to align with the upper illustration on the main figure. Edit the html file (add "<h1>M4</h1>
") and run the git commit
command. To check the latest status, run the git log
command again.
<!-- Master Branch-->
<h1>M1</h1>
<h1>M2</h1>
<h1>M3</h1>
<h1>M4</h1>
<!-- /Master Branch-->
git commit -am "M4"
git log --oneline
Finally, you'll see the following status, which is aligned with the upper illustration on the main figure for the master branch.
b25d998 (HEAD -> master) M4
e12beda M3
f5ff7aa M2
8bf5c03 M1
As we haven't touched Branch_A, its status remains the same.
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.
git merge Branch_A
In the Non-Fast-forward case, the command line goes into the interactive mode while a text editor is launched. A text editor is used to write a commit message. A simple message is already written. Unless you want to add another 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 as shown 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.
* ececccc (HEAD -> master) Merge branch 'Branch_A'
|\
| * a4c4eb0 (Branch_A) A2
| * 62dda63 A1
* | b25d998 M4
|/
* 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