Rebase Branch – Git Rebase
git rebase
is the command used to reapply all the commits made on the topic branch on top of the HEAD of the base branch (the branch that the topic branch was separated from). This command is useful when you want to streamline commits made on multiple branches. The rebase operation can also be done through a Remote Repository on the GitHub platform. It is an option of the merge feature of GitHub.
In the command line, you need to run the command on the branch to be rebased (a divergent branch) and specify its base branch name as a command parameter.
git rebase [base branch name]
The rebase command changes the divergence point to the HEAD of the base branch. It doesn't impact the base branch itself. If you want to integrate the changes into the base branch (e.g., the master branch), you need to run the git merge
command on the base branch side. The merge action will be the Fast-Forward merge as the divergent branch is already ahead of the HEAD of the base branch.
For a better understanding, please go through the following practice section.
Practice
Developer A (Project Owner Role)
Objective:
Learn how to rebase a branch
1. Prepare a practice file
In the practice on the previous page, we merged Branch_A with the master branch using the squash option. 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 M4, reset the merge operation by running the git reset --hard
command on the master branch. Use the commit hash of commit M4 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 b7b73d4
git log --oneline
You'll see that the HEAD of the master branch is back to Commit M4.
b7b73d4 (HEAD -> master) M4
e12beda M3
f5ff7aa M2
8bf5c03 M1
Next, create commit M5 to match the illustration on the left of the main figure. Edit the HTML file (add <h1>M5</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>
<h1>M5</h1>
<!-- /Master Branch-->
git commit -am "M5"
git log --oneline
Finally, you'll see the following status, which matches the illustration on the left of the main figure for the master branch.
288068f (HEAD -> master) M5
b7b73d4 M4
e12beda M3
f5ff7aa M2
8bf5c03 M1
Check the status of Branch_A.
git checkout Branch_A
git log --oneline
As we haven't touched Branch_A in this practice yet, its status remains the same as the one in the previous practice. Branch_A has diverged at commit M3 of the master branch, which is aligned with the original position of the Topic Branch on the illustration on the left of the main figure.
a4c4eb0 (HEAD -> Branch_A) A2
62dda63 A1
e12beda M3
f5ff7aa M2
8bf5c03 M1
2. Run the rebase command
Now you are ready to execute the rebase command. As you are already on Branch_A, run the following command.
git rebase master
You can see that Branch_A was successfully rebased.
Successfully rebased and updated refs/heads/Branch_A.
To check the latest status of Branch_A, run the git log
command.
git log --oneline
Now you can see that Branch_A was rebased to the HEAD of the master branch (commit M5). In this operation, you can also confirm that the commit hashes for commits A1 and A2 are different from the original ones although the committed file versions remain the same.
479f994 (HEAD -> Branch_A) A2
6d96103 A1
288068f (master) M5
b7b73d4 M4
e12beda M3
f5ff7aa M2
8bf5c03 M1
3. Run the merge command
The rebase command changed the Branch_A status, however, the master branch stays the same; its HEAD is still at commit M5.
git checkout master
git log --oneline
288068f (HEAD -> master) M5
b7b73d4 M4
e12beda M3
f5ff7aa M2
8bf5c03 M1
If you want to integrate the change in Branch_A into the master branch, you need to run the merge command.
git merge Branch_A
In this case, the merge operation becomes Fast-forward as Branch_A was reattached to the HEAD of the master.
Updating 288068f..479f994
Fast-forward
git_branch_practice.html | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
To check the latest status of the master branch, run the git log
command.
git log --oneline
Now you can see that the HEAD of the master branch moved forward to the same position as the HEAD of Branch_A; the statuses of the master branch and Branch_A became the same.
479f994 (HEAD -> master, Branch_A) A2
6d96103 A1
288068f M5
b7b73d4 M4
e12beda M3
f5ff7aa M2
8bf5c03 M1