iT邦幫忙

0

Git-使用本地分支與遠端分支達成協同作業

git
WM 2019-05-17 16:01:001198 瀏覽
  • 分享至 

  • twitterImage
  •  

延續Git-上傳檔案至遠端儲存庫

模擬一個協同作業的環境

將GitHub的專案,clone至指定資料夾(ngapp2)。
https://ithelp.ithome.com.tw/upload/images/20190515/20112573akheCXSgZd.png

ngapp代表同事A,ngapp2代表同事B。

ngapp的分支。
https://ithelp.ithome.com.tw/upload/images/20190515/201125735vGmjJV94U.png

ngapp2的分支。
https://ithelp.ithome.com.tw/upload/images/20190515/20112573dzIjw4JiOR.png
remotes/origin/HEAD -> origin/master分支,可以不用理它。

會發現,ngapp2比ngapp少了2條本地分支(br3、feature)。

這種狀況不用太在意,我們所要做的是,確認要在哪條分支開發。

假設我們想要在ngapp2開發一個新功能,並且從feature分支開發。

先切換至feature分支。
https://ithelp.ithome.com.tw/upload/images/20190517/20112573KZnEzInAp2.png
這時,有個疑問,我們的本地端並沒有feature分支。
那可以切換至remotes/origin/feature分支嗎?
當然不行,原因Git-本地分支、遠端分支、遠端追蹤分支說過了。

直接切換至feature分支,看看會有什麼結果。
https://ithelp.ithome.com.tw/upload/images/20190515/20112573ZrbUPlAqsr.png
由於git找不到本地feature分支,它會找到remotes/origin/feature與feature分支同名的遠端追蹤分支。
建立feature分支,並將remotes/origin/feature設為feature分支的遠端追蹤分支。

但如果切換至feature1分支,會發生錯誤。
https://ithelp.ithome.com.tw/upload/images/20190517/20112573WaGtHtyJe5.png
因為git找不到名為feature1的遠端追蹤分支。

建立br1分支,新增檔案,再commit。
此時,br1是最新版的分支。
https://ithelp.ithome.com.tw/upload/images/20190517/20112573Q6kGpCdGtP.png

取得他人的分支

但目前這個分支只存在於同事B的本地端,同事A並沒有這個分支。

那要如何讓同事A能夠取得br1分支呢?

  1. 先將br1 push至遠端儲存庫。
    https://ithelp.ithome.com.tw/upload/images/20190517/20112573hRCWX2ZGIR.png

  2. 遠端儲存庫確實有br1分支。
    https://ithelp.ithome.com.tw/upload/images/20190517/20112573SwDmStZC0k.png

  3. 在ngapp確認分支情況。
    https://ithelp.ithome.com.tw/upload/images/20190517/20112573I8s2QvmV2f.png
    ngapp沒有br1的遠端追蹤分支,那要如何取得呢?

  4. 使用fetch取得更新。
    https://ithelp.ithome.com.tw/upload/images/20190517/20112573ON9GVyBcVI.png
    訊息顯示,取得br1的遠端追蹤分支。

  5. 再看一次分支情況。
    https://ithelp.ithome.com.tw/upload/images/20190517/20112573kXh96OS2Y5.png
    OK,已經取得br1的遠端追蹤分支。

  6. 跟剛剛的方式一樣,取得br1分支。
    https://ithelp.ithome.com.tw/upload/images/20190517/20112573NtwHFCX5KY.png

  7. 執行git log。
    https://ithelp.ithome.com.tw/upload/images/20190517/20112573uz9Xofrj2S.png

  8. ngapp2再執行一次git log。
    https://ithelp.ithome.com.tw/upload/images/20190517/20112573gNDZQa8IPg.png
    這次,雙方版控情況,完全相同。

協同開發步驟

  1. 先建立新分支,開發新功能。
  2. push新分支。
  3. 讓他人透過fetch取得遠端追蹤分支。
  4. 在電腦建立遠端追蹤分支。
  5. 藉由遠端追蹤分支建本地分支。
  6. 繼續在新建立的分支開發或合併分支。

本文為觀看網路教學的學習筆記。


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

尚未有邦友留言

立即登入留言