iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 25
0
自我挑戰組

Experience of a backend novice系列 第 25

功能開發完畢,要如何合併回主線呢?歡迎git merge,快轉merge

Day twenty-five

功能開發完畢,要如何合併回主線呢?歡迎git merge,快轉merge
Let's use git merge to incoporate the test branch into master branch,fast-forward merge

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.
前面幾篇已大略講了git的架構以及物件,從今天開始我會繼續分享git更進一步的應用,相信大家在對架構以及物件有基本的了解之後,對於之後的應用會更容易了解。

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.
前面講到了分支(branch),當我們已經完成一個完整的功能,而想要在開發一個新的功能時,我們可以開一個分支,然後在上面開始新功能的開發,隨著新功能裡面的小功能一步步的完成,我們也會在這個新功能分支上有很多的commit,直到最後我們完成了整個新功能。

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.
這時候我們就需要使用git merge來合併兩個分支。

Let's grok it through hand-on experiment.
讓我們通過實作來理解這個概念。

Firstly let's go to 'my-git-repository'
首先回到我們的my-git-repository.

Based on where we were, the master history should look like the follows:
根據昨天的進度,master分支的歷史如下:

Through the image above, we could see that master branch, vegetable branch point to the same commit.
由上圖可以看到,master分支與vegetable分支都在同一個commit上

Enter:
輸入:
git branch

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.
由上圖可以看到,我們目前有三個分支,因為meat分支的進度要領先於master以及vegetable分支,所以我們在master的歷史上看不到meat分支。

Let's switch to meat branch.
現在讓我們切到meat分支:

git checkout meat
git log --oneline

Through the image above, we could see that currently the HEAD is on meat branch.
由上圖可以看到,我們現在的HEAD已經在meat分支上。

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.
這個指令目前看不懂沒有關係,之後會再做解說,重點我們可以從上圖中看到,meat分支比master分支多了一個檔案'fileOnlyExistingInMeatBranch'。

Okay. Now we want the master branch also possess this file. Before merging, let's take a look on current log.
好,現在我們要讓主線master也擁有這個檔案。
在合併之前,讓我們先來看看目前的狀態:

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.
上圖可以看到,目前HEAD在meat上,那我們現在準備將HEAD切換回master分支,然後再合併meat分支。

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.
上圖可以看到,我們已經合併了兩個分支,現在master分支裡面也有了原本只屬於meat分支的檔案。

Through photo above we could see that now the master branch and meat branch are both pointing to the same commit.
上圖可以看到,現在master分支的跟meat分支在同一個commit上。

Currently Git looks like this:
目前Git狀態大概如下:

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.
當我們merge時,如果其中一分支的HEAD位於兩個分支共同的起點,如同此次我們merge的情況,那就會觸發所謂的快轉merge。

So what's fast forward merge?
什麼是快轉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.
想想我們之前提到的git架構以及流程,當我們做任何檔案變更,這邊的變更包含新增或修改,Git都會產生相對應的blob以及tree物件來對應進行該變更的commit。而當兩個分支擁有共同的起點,且只有其中一個分支有所變更,這代表著當我們合併時,Git不需要產生額外的commit來指向特定的tree物件以及blob物件,Git只需要將原本停留在共同起點的分支上的HEAD,移動到另外一個有變更的分支的commit上,這樣就算是完成了這一次的合併。

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.
簡單來說,當兩個分支擁有共同起點,且只有其中一個分支自共同起點開始有所變更,那當我們合併時,Git只需要將未變更的分支上的HEAD移動到有修改過的分支的HEAD上,這樣的合併並不會產生額外的commit來指向特定的tree以及blob物件,稱為快轉合併(fast-forward merge)

Hope you guys like my article today, see you tomorrow.
希望今天的分享可以讓大家更了解Git,我們明天見。


上一篇
Git的物件,當我們commit時,究竟發生了什麼事4
下一篇
Git merge,三路合併的概念
系列文
Experience of a backend novice30

尚未有邦友留言

立即登入留言