iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 26
0

Day twenty-six

Git merge,三路合併的概念
Git merge, three way merge concept

Hello everyone, it's Ray.
大家好,我是Ray

Yesterday I shared fast-forward merge. Today I'm going to share anohter merge called three-way merge.
昨天跟大家分享了git merge的fast-forward merge,今天我將跟大家分享另一種模式的merge, 三路合併。

Currently our git looks like image above
目前我們git如上圖。

Now let's switch to vegetable branch, and add a new file 'fileOnlyExistingInVegetableBranch.html'
現在讓我們切換到vegetable 分支,並且新增一個名為'fileOnlyExistingInVegetableBranch.html'的檔案。

git checkout vegetable
touch fileOnlyExistingInVegetableBranch.html
git add *
git commit -am 'add a file only existing in vegetable branch'
git log --oneline

Now git looks like image below:
現在git大概如下:

ls

Through image below, we could see that the files exisiting in this branch.
如下圖可以看到目前這個分支上存在的檔案。

Currently the HEAD is pointing to vegetable branch, and nwo we are going to merge master branch from vegetable branch.
目前HEAD位於vegetable分支上,且現在我們將從vegetable分支來合併master分支:

git merge master

As image below, a window pops up, and it allows us to leave some message for this commit.
如下圖,應該會有一個視窗跳出,這個視窗我們可以留下關於這次merge的相關資訊。

No need to change the default message, just enter wq and then leave.
不需更改預設訊息,直接wq儲存離開即可。

:wq

Did you notice that? In my last article, we a fast-forward merge was triggered, this window didn't pop up.
注意到了嗎? 上一篇文章中,當我們觸發快轉合併,這個訊息視窗並沒有出現哦!

So why it popped up this time?
那為什麼現在出現了呢?

Let's take a look on what git looks like now:
先來看看現在git的視覺圖:

Through the image above, we could see that a new commit has been created, as mentioned previously about git's structure and objects, pointing to new tree objects, which is pointing to those blob objects possessed by master branch and vegetable branch originally.
由上圖可以看到,一個新的commit產生了,如同之前git的基本架構與物件章節中提到的,這個新產生的commit指向新的tree物件,而這個tree物件指向了原本兩個分支分別指向的物件,所以在這個新產生的commit上,會有
擁有分別只屬於vegetable分支以及master分支上的檔案。

ls

As image above, 'fileOnlyExistingInMeatBranch.html' only existed in meat branch, but in my last article, we merged meat branch from master branch, so the master branch possessed this file that had only existed in meat branch.
如上圖,'fineOnlyExistingInMeatBranch.html'原本只存在meat分支上,但在上一個章節我們從master分支上合併了meat分支,所以我們的master分支上有了這個原本只存在於meat分支上的檔案。

And now we merged master branch from vegetable branch, so the vegetable branch not only possessed the files it originally held, but also those had only existed in master branch, like 'fileOnlyExistingInMeatBranch.html'
而現在我們從vegetable分支上合併了master分支,所以在vegetable分支上除了保有原本的檔案之外,也有了'fileOnlyExistingInMeatBranch.html'這個檔案。

In order to merge two branches, git produced a new commit pointing to two different branches, and this kind of merge is called 'three-way merge'
Git為了合併兩個分支,產生了一個新的commit來指向兩個不同的分支,這樣的合併在git中稱為三路合併(three way merge)

Let's call it today, see you guys tomorrow.
今天的分享就到這裡,我們明天見!


上一篇
功能開發完畢,要如何合併回主線呢?歡迎git merge,快轉merge
下一篇
git遇到衝突了怎麼辦?別緊張,解衝後就好了。
系列文
Experience of a backend novice30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言