iT邦幫忙

0

Git-本地分支、遠端分支、遠端追蹤分支

git
WM 2019-05-15 12:55:303525 瀏覽

本地分支
位於本地端的分支。
https://ithelp.ithome.com.tw/upload/images/20190515/20112573U1ihAkzWho.png

遠端分支
位於遠端儲存庫的分支。
這是我們之前push的遠端儲存庫,上面的分支就是遠端分支。
https://ithelp.ithome.com.tw/upload/images/20190515/20112573Jum1aEP8wA.png

遠端追蹤分支
設置於本地端,讓本地分支與遠端分支產生連結的分支。
換句話說,它是本地分支與遠端分支的橋樑。
https://ithelp.ithome.com.tw/upload/images/20190515/201125736e2H0ffgAd.png

嘗試切換至遠端追蹤分支。
https://ithelp.ithome.com.tw/upload/images/20190515/20112573avy5TA4Q5v.png
會收到一長串的警告訊息。
其中一段告知,你目前正處於detached HEAD的狀態,這是一個不正常的情況。
注意,遠端追蹤分支不是用來開發的,它是用來追蹤的。

請不要切換至遠端追蹤分支,一律切換至本地分支。

遠端追蹤分支的意義

目前GitHub的feature分支有18個commit,我們打算在本地端增加一個commit,將它push到GitHub。
https://ithelp.ithome.com.tw/upload/images/20190515/20112573y8PhAI2O2t.png

push完成,目前有19個commit。
https://ithelp.ithome.com.tw/upload/images/20190515/20112573EniLEtNOuw.png

push的過程是這樣的:
當feature分支增加一個commit,執行push後,git會尋找feature分支所對應到的遠端追蹤分支,也就是remotes/origin/feature,這個路徑的來源,等下會做說明。
找到後,將commit上傳至遠端追蹤分支所指向的遠端儲存庫分支feature。

遠端追蹤分支設定檔

設定檔位於.git資料夾裡面的config。
https://ithelp.ithome.com.tw/upload/images/20190515/20112573yjBGCTuklF.png

master分支的設定。

https://ithelp.ithome.com.tw/upload/images/20190515/20112573eWQIC6JFS6.png

  • remote表示這個分支對應到的遠端位置。就是origin。
  • merge表示合併會使用的本地分支。位於refs/heads/master。

origin的設定。

https://ithelp.ithome.com.tw/upload/images/20190515/20112573PF3iopSubO.png

  • url表示origin所指向的位置。
    之前執行指令「git remote add origin https: //github.com/s164975/ngapp.git」時設定的。
  • fetch表示本地分支與遠端追蹤分支的對應。
    星號(*),表示本地分支與遠端追蹤分支同名。

查看.git/refs/heads/資料夾。
https://ithelp.ithome.com.tw/upload/images/20190515/20112573FnWmQzJF6O.png
有3個檔案,所代表的正是本地端分支。

查看.git/refs/remotes/origin/資料夾
https://ithelp.ithome.com.tw/upload/images/20190515/201125738onVmOWQDu.png
有2個檔案,所代表的是遠端追蹤分支。

所以,本地分支與遠端追蹤分支的對應關係就是在fetch做定義的。
https://ithelp.ithome.com.tw/upload/images/20190515/201125739jHICyswV6.png

一般來說,本地分支與遠端追蹤分支的名稱最好相同,避免搞混。


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言