iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 24
1
自我挑戰組

Experience of a backend novice系列 第 24

Git的物件,當我們commit時,究竟發生了什麼事4

  • 分享至 

  • xImage
  •  

Day twenty-four


Git的結構與commit的實際流程4

How is commit made? What’s git’s structure? part 4

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

Let's continue what we haven't done yet, the last two:
延續昨天的進度,今天我們要來介紹最後的兩個:

  1. What is branch? / Branch是什麼?
  2. What is Head? / Head是什麼?

Firstly, what's branch?
首先,什麼是branch?

Actually branch is very similar to tag, a file with 40 characters within, pointing to a commit.
So what's the difference between them?
其實branch跟tag很像,就只是一個檔案,裡面有一串數字,一樣是指向commit。那這兩者的分別是什麼呢?

Good question! Acutally the only difference is that branch would move with the new commit, and tag only stays where we tag.
好問題! 其實兩者的分別就只是... branch會隨著我們commit而跟著新的commit跑,而tag則是不會移動,會永遠待在我們tag的commit上。

As just mentioned, branch is just a file pointing to a commit. Ray, can you explain it more explicitly?
剛剛提到,branch就只是一個指向commit的檔案,那有沒有辦法可以更明確一點的看到這個檔案呢?

No problem! Let's engrave knowledge onto our brain by hand-on experiment.
沒有問題。讓我們通過實作來將知識烙印在我們的腦海中!

Let's go back to 'my-git-repository'. If you does follow our progress, it should look like the image below:
讓我們回到my-git-repository,如果你有跟著我們之前的進度走的話,目前的進度應該如下:

Enter:
輸入:

git log --oneline
git branch meat
git branch vegetable
ls .git/refs/heads

As image below, we could see that in the heads folder under refs folder under .git folder, there are three files. One denotes master branch, another represent vegetable branch, and the other is for meat branch.
如下圖,我們可以看到,在我們.git資料夾裡的refs資料夾裡的heads資料夾內,嗯...好像繞口令... ,有三個檔案,一個代表我們原本的master branch,兩個是剛剛建立的vegetable以及meat branch。

Let's take a look on what are inside them, enter:
現在讓我們來看看這些檔案裡面都放了什麼,輸入:

cat .git/refs/heads/master
cat .git/refs/heads/vegetable
cat .git/refs/heads/meat

As image above, we got three sets of exactly same alphanumeric numbers
如上圖,我們得到三組一模一樣的號碼!

As mentioned above, branch is actually a file pointing to the current commit, and so...
上面有提到,其實branch只是一個指向當前commit的檔案,那...

Let's take a guess that those three sets of alphenumeric number will be exactly the same as the current commit's
未看先猜,我猜這三組號碼會跟我們git log 當前commit的數字一樣!

Enter:
輸入:
git log --oneline

Here you go!
登登!!

The alphanumeric number of the current commit is exactly the same as those we got from three branches, which attest that all of them are pointing to the current commit.
當前commit跟我們剛剛從三個branch取得的號碼完全一模一樣,這代表著當前三個檔案都指向當前的commit

Now let's checkout to branch, and then commit, and see if the alphanumeric number will change
現在讓我們切換到meat branch,然後commit,再來看看裡面的號碼會不會有什麼變化。

Enter:
輸入:

git checkout meant
touch fileOnlyExistingInMeatBranch.html
git add *
git commit -am "an experimental commit in meat branch"
git log --oneline

Take a look on the current log as follows:
看看目前的歷史紀錄,如下:

As image above, currently we are on meat branch, and make a new commit, and Git produce a whole new and unique alphanumeric number for it.
由上圖可以看到,目前我們在meat的branch上,並且新增了一個commit,而新增的commit自己的一組新的號碼。

Now let's check if there is any change on those branch files.
現在讓我們來看看剛剛的三個branch檔案裡面有什麼樣的變化。

Take a guess before checking!
I guess that the alphanumeric number inside the mean branch file will be exactly the same as the newly created commit's, and because vegetable branch and master branch are still pointing to the old commit, so there is no change in them.
未看先猜,我猜名為meat的branch檔案裡面的號碼會變得跟我們剛新建的commit一樣,而vegetable以及master的branch檔案因為仍指向舊的commit,所以不會有變化。

Time to testify
驗證的時間到了:

cat .git/refs/heads/master
cat .git/refs/heads/vegetable
cat .git/refs/heads/meat

There you go!
登登!

So the conclusion is that branch is just a file pointing to the newest commit on it with 40 characters inside.
所以結論就是,branch就只是一個裡頭有個40個字元,並且指向該branch裡頭的最新的commit的一個檔案!

Okay. We've done with branch. Let's go for HEAD. What the heck is HEAD?
好了,我們已經解決掉branch了,那該到HEAD了,什麼是HEAD?

HEAD is also just a file point to current branch.
HEAD也只是一個檔案,用來指向當前切換的branch。

In the .git folder, there is a file named as HEAD, which indicates which branch we are current on.
位於.git資料夾內,有一個名為HEAD的檔案,檔案內就明確說明我們當前位於哪一個branch上面。

We are still on meat branch, so let's take a look on the file, to see if there is any information related to mean branch.
我們最後的操作在於meat的branch上,讓我們來看看該檔案內是不是有相關於meat branch的資訊。

Enter:
輸入:
cat .git/HEAD

Here you go!
登登登!

As above image, it indicates that it's pointing to meat.
上圖可以看到,檔案裡面說明著目前正指向meat。

Some pople might say "Would it jsut be an coincidence?"
那有些人會說,啊~你這會不會只是湊巧啊?

There is nothing I can do with it but make anohter experiment.
那我只好再做一次實驗囉!

Enter:
輸入:
git checkout vegetable
cat .git/HEAD

Here you go!
登登登登!!

As photo above, as long as the current branch changes, the information in the HEAD file will change accordingly, and it indeed repredents the HEAD in Git.
由上圖可以看到,只要當前的branch變了,該檔案內的資訊就會跟著改變,而這個檔案也正代表著git裡頭的HEAD。

The conclusion is that HEAD serves as an index pointing to the current branch.
結論就是,HEAD就是一個指向當前branch的一個指標。

So far, we've talked about the objects in Git, and its rough structure.
截至本篇,大概已經介紹完了git裡頭的物件,以及大略的架構。

In the later module, we will continue to share deeper usage. See you guys tomorrow.
接下來會繼續跟大家分享Git的一些更深層的應用,我們明天見!


上一篇
Git的物件,當我們commit時,究竟發生了什麼事3
下一篇
功能開發完畢,要如何合併回主線呢?歡迎git merge,快轉merge
系列文
Experience of a backend novice30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言