Fast-Forward Merge
As explained, when you are merging a child branch that is simply ahead of the parent branch (no changes were made to the parent branch after the point when you created the child branch), the git merge
command simply brings changes made in the child branch into the parent branch without creating a new commit. This merging approach is called fast-forward merge.
In the fast-forward merge, the status of the branches will shift to the one shown in the main figure. This command moves the HEAD of the master branch forward to the point of the HEAD of Branch_A. The merge command doesn't impact Branch_A. Simply, the master branch becomes the same as the status of Branch_A.
We'll explain the 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 that were already made before. We'll explain the commands and responses in the command line in four steps.
- Confirm the original commit history status by running the
git log
command on Branch_A. You can see that the commit histories and branch statuses are the same as the ones illustrated in the main figure. - Switch to the master branch and run the
git merge Branch_A
command. You can see that this merge is Fast-forward. - Confirm how the commit history looks on the master branch by running the
git log
command. You can see that the HEAD of the master branch has moved forward to the same position as the HEAD of Branch_A. - To confirm the status of Branch_A, switch to Branch_A and run the
git log
command. You can see the same result as the one in the master branch. This means that Branch_A and the master branch are in the same status now.
For a better understanding, please go through the following practice section.
Practice
Developer A (Project Owner Role)
Objective:
Check how the Fast-Forward merge works
1. Prepare a practice file
In this practice, we'll modify the file and make some commits M1, M2, M3, A1, and A2. In the previous practice, we have already created the git_branch_practice.html file under the git_branch_practice directory. As we already deleted Branch_A and Branch_B, you have only the master branch now. The code on the master branch is like the one below.
<!doctype html>
<html lang="en">
<body>
<!-- Master Branch-->
<h1>M1</h1>
<!-- /Master Branch-->
<!-- Branch_A-->
N/A
<!-- /Branch_A-->
<!-- Branch_B-->
N/A
<!-- /Branch_B-->
</body>
Make new commits on the master branch
To align with the upper illustration in the main figure, let's make commits M2 and M3 first.
Add M2 under the html file and save it. Then, make a commit with "M2" as a commit message.
<!-- Master Branch-->
<h1>M1</h1>
<h1>M2</h1>
<!-- /Master Branch-->
git commit -am "M2"
[master f5ff7aa] M2
1 file changed, 1 insertion(+)
Repeat the same actions for commit M3.
<!-- Master Branch-->
<h1>M1</h1>
<h1>M2</h1>
<h1>M3</h1>
<!-- /Master Branch-->
git commit -am "M3"
[master e12beda] M3
1 file changed, 1 insertion(+)
Create a Branch_A and make new commits
Next, create Branch_A and checkout to this branch.
git checkout -b Branch_A
Switched to a new branch 'Branch_A'
To make commit A1, edit the file and save it first.
<!-- Branch A-->
<h1>A1</h1>
<!-- /Branch A-->
Make commit A1.
git commit -am "A1"
[Branch_A 62dda63] A1
1 file changed, 1 insertion(+), 1 deletion(-)
Repeat the same actions for commit A2.
<!-- Branch A-->
<h1>A1</h1>
<h1>A2</h1>
<!-- /Branch A-->
git commit -am "A2"
[Branch_A a4c4eb0] A2
1 file changed, 1 insertion(+)
Run the git log
command to see the commit history.
git log --oneline
You'll see the following status shown below. Commit hash is a unique number generated by the computer each time. You'll see different ones on your computer, however, the structure of commit histories and branch status will be the same.
a4c4eb0 (HEAD -> Branch_A) A2
62dda63 A1
e12beda (master) M3
f5ff7aa M2
8bf5c03 M1
2. Perform the merge commit
As you need to run the merge command from the master branch, first switch the current branch to the master branch. Then, run the merge command.
git checkout master
git merge Branch_A
The command line response shows that the Fast-forward merge is executed.
Switched to branch 'master'
Updating e12beda..a4c4eb0
Fast-forward
git_branch_practice.html | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
3. Check commit histories and branch status
Check the status on the master branch first.
git log --oneline
You can see that the HEAD of the master branch moved forward to the same position as the HEAD of Branch_A. Also, you can confirm that no new commit is created by this operation.
a4c4eb0 (HEAD -> master, Branch_A) A2
62dda63 A1
e12beda M3
f5ff7aa M2
8bf5c03 M1
Next, check the status on Branch_A.
git checkout Branch_A
git log --oneline
You can see exactly the same results as the ones on the master branch. This means that the master branch and Branch_A are in the same status now.
a4c4eb0 (HEAD -> Branch_A, master) A2
62dda63 A1
e12beda M3
f5ff7aa M2
8bf5c03 M1