In the previous articles I roughly mentioned Git's structure and its objects, and from now onwards, I will share more about how to use Git, and I believe that knowing its structure and how it acutally works will bring more understanding when we learn how to use it.
As mentioned previously about branch, when we complete a sound function, and we would like to develop anohter one based on this, we could create a new branch and start developing new function on it, and we could add more commits as small function completed until we complete the whole one.
And in this case, how could we add the newly developed branch into the master branch?
In this case, we could we
git merge to merge two branches.
Let's grok it through hand-on experiment.
Firstly let's go to 'my-git-repository'
Based on where we were, the master history should look like the follows:
Through the image above, we could see that master branch, vegetable branch point to the same commit.
Through image above, we could see that courrently we have three branches, but because the commit of meat is ahead of the commit on master and vegetable branches, we can't see the meat branch in the master history.
Let's switch to meat branch.
git checkout meat
git log --oneline
Through the image above, we could see that currently the HEAD is on meat branch.
git diff master..meat
If you don't understand what it means, don't panic. I will explain it in later module. The point is that we could see that there is one more file in meat branch than master branch.
Okay. Now we want the master branch also possess this file. Before merging, let's take a look on current log.
From image above you could see that now the HEAD is pointing to meat, and now we are going to switch back to master ,and then merge meat branch.
git checkout master
git merge meat
As photo above, we could see that we've merged two branches. Now the file originally only in meat branch also occurs in master branch.
Through photo above we could see that now the master branch and meat branch are both pointing to the same commit.
Currently Git looks like this:
Here is one very important thing that I would like to share with you.
When merging, if one of the two branches points to their mutual forking point, then it would turn out as fast-forward merge.
So what's fast forward merge?
Think about the Git's structure and actual process that we have mentioned. When making any change on files, the change I am talking about here includes any adding or revising, Git will produce correspoinding blob and tree objects for the commit. When two branches share with the same forking point, and only one of them changes, it means that Git doens't have to produce additional commit pointing to specific tree and blob objects, instead, Git only has to move the unchanged branch from mutual forking point and point to the commit on the changed branch, and that represents a complete merge.
Simply speaking, when two branch share with a mutual foring commit, and only one of them changes from the forking commit, and then when merging them, Git only has to make the unchanged branch point to the commit of changed branch, and merging in this way doesn't require additional commit to point to specific tree of blob objects, which is called as fast-forward merge.
Hope you guys like my article today, see you tomorrow.