This is a 100 Days challenge to learn a new language (Python). 100 Days of Code - The Complete Python Pro Bootcamp
I will post some notes to motivate myself to finish this challenge.
A software development enthusiast who has a passion for new technology. I will post things that are interesting or tasks I have encountered.
This is a 100 Days challenge to learn a new language (Python). 100 Days of Code - The Complete Python Pro Bootcamp
I will post some notes to motivate myself to finish this challenge.
This is a 100 Days challenge to learn a new language (Python). 100 Days of Code - The Complete Python Pro Bootcamp
I will post some notes to motivate myself to finish this challenge.
We should discuss the difference between fast-forward and the 3-way merge before introducing the 'git rebase'
When the merging branch (bugFix) has all commits from the current branch (master), Git will not create a new merging commit. Instead, it will move the 'master' branch pointer to the latest commit of the 'bugfix' branch. It is a line (linear path) relationship.
When the current branch (master) contains commits that the merging branch (bugfix) doesn't have, Git will create a new merging commit that merges two branches without modifying the existing branches. It is a tree (with a cycle) relationship.
Therefore, if we don't want to make it to create a new commit that is used for merging two branches, then we need to try to make the git structure to be Fast-Forward. Then the 'git rebase' command can help!
Copies commit on top of another branch without creating a commit, which keeps a linear history. Git will delete the old changes and rewrite them (with new sha1) on the top of the latest target (master) branch.
// Each branch has a new commit.
(master)
C1 -> C2
-> C3
(bugfix)
// After rebase, it turns into linear history
(master)
C1 -> C2
-> C2 -> C4 (rewritten commit from C3)
(bugfix)
NOTE:
1. Conflicts might be happened as usual, and we need to resolve them manually.
2. Rebase is not suitable if multiple people use the same branch.
3. The master branch is better not to do rebase
Below are the logs from my experiment (Rebasing without conflicts)
Check the status of the master branch
Switch to the bugFix branch and check the status.
Check the git structure.
Run the 'git rebase' command from master in the current bugFix branch.
According to the message above, the commit 'first commit in bugfix' will be rewritten.
Check the status of the bugFix branch.
1. Now, the new commit cd1374 from the master branch is copied to the bugfix branch.
2. You will notice that the sha1 of 'first commit in bugfix' is changed from '38b42c' to '0238eb'.
Compare the git structure.
Check the rewritten commit.
Switch to the master branch and do the 'git merge' command.
If it is not the linear path, then we need the 3-way merging.
First of all, create a new branch called bugFix from the master branch, and add a new commit called C4 to it.
---------------------------------------------------------------------------------
(master)
C1 -> C2 -> C3
-> C4
(bugFix branch)
Before merging the C4 commit to the bugFix branch, the master branch has added a new commit called C5. From the chart below, it is not the linear path anymore.
---------------------------------------------------------------------------------
(master) C1 -> C2 -> C3 -> C5 -> C4 (bugFix branch)
Use the 'git merge' command to merge the bugFix branch to the master branch.
---------------------------------------------------------------------------------
(master) C1 -> C2 -> C3 -> C5 -> C6 -> C4 -> (bugFix branch)
The 'git merge' command will help to create a new commit C6 and its parents are C4 and C5.
Below are the logs from my experiment (3-way merging without conflict)
Check the status of the bugFix branch.
Switch to the master branch and show the status.
Run the 'git merge' command to merge the bugFix branch to the master branch.
It is the auto-generated commit coming from the 'git merge' command.
Check the status.
We notice that there are two parents in this commit object!!
Check the first parent info that shows that it is the latest commit from the master branch.
Check the second parent info that shows that it is the latest commit from the bugFix branch.
Fast-forward is the simplest scenario for 'git merge', but it is a rare case in reality.
The 'fast forward' required that there is no new commit in the 'master' branch.
For instance, in the beginning, the latest commit of the master branch is c3.
---------------------------------------------------------------------------------
(master branch)
C1 -> C2 -> C3
Then create a new branch called bugFix, and it points to the C3 commit.
---------------------------------------------------------------------------------
(master branch)
C1 -> C2 -> C3
(bugFix branch)
Then, later on, a new commit, c4, is created in the bugFix branch.
---------------------------------------------------------------------------------
(master branch)
C1 -> C2 -> C3
-> C4
(bugFix branch)
After fixing bugs in the bugFix branch, we want to merge our work to the master branch.
There is no new commit in the master branch, and it meets the 'fast forward' requirement.
---------------------------------------------------------------------------------
C1 -> C2 -> C3
-> C4
(bugFix branch)
(master branch)
If it is fast-forward case, then the 'master' branch pointer will move to the last commit of the 'bugfix' branch.