iT邦幫忙

0

Git-合併分支

git
WM 2019-05-11 02:24:294087 瀏覽
  • 分享至 

  • twitterImage
  •  

準備工作

強烈建議使用Git GUI工具來觀察分支,有助於釐清目前專案的版控情況。

下載TortoiseGit,安裝很簡單,就不多說明了。

安裝完畢,打開軟體,在左下角勾選All Branches,才能顯示所有分支的情況。
https://ithelp.ithome.com.tw/upload/images/20190510/201125730OuUN27R1X.png

觀察分支

延續Git-了解分支&分支(建立、切換、刪除、回復)
顯示master與feature分支。
initial commit是所有分支的共同commit,再以此分出去。

master分支
https://ithelp.ithome.com.tw/upload/images/20190510/20112573GAChMYB7ch.png

feature分支
https://ithelp.ithome.com.tw/upload/images/20190510/20112573yVUr9Taotd.png

合併前注意

合併分支之前,有兩點要先考量:

  1. 先確定主要分支,再合併其他分支。
  2. 合併之後,可能發生的衝突。

假設我們以master為主要分支,去合併feature分支。
重點注意,合併之後,會修改master分支的commit,而feature分支不受影響。
換句話說,master分支會套用feature分支所有的更新。

補充說明
master分支與feature分支,在git中都只是分支,沒有誰是主要分支的問題。
這只是一般開發習慣,會將master視為主要分支。
所以,我們也可以反過來,將master併入feature之中。
只是很少這樣做。

開始合併

  1. 設定master為主要分支,合併feature分支。
  2. 重點注意,必須要在master分支執行。
  3. 合併指令 git merge [分支名稱]
  4. 執行後,會跳出Vim文字編輯器,只要輸入:q按下ENTER,就可以儲存訊息離開。
    https://ithelp.ithome.com.tw/upload/images/20190510/20112573mxKtWLHxoo.png
    關於Vim的操作,請自行Google。

顯示log小技巧
可以輸入 git log --oneline
表示只要簡短的commit歷程,畫面會清爽許多。
https://ithelp.ithome.com.tw/upload/images/20190510/20112573FXM5YvHl7k.png
成功合併後,master分支會多一個合併的commit,訊息顯示 Merge branch 'feature'
同時也看到一個來自feature的commit,Id等同於feature的commit。

合併後的線圖。
https://ithelp.ithome.com.tw/upload/images/20190519/20112573qUPpOerrcG.png
feature分支被併入master分支,並且產生一個合併的commit(Merge branch 'feature')。

目前位於master分支,查看工作資料夾,src資料夾消失了。
https://ithelp.ithome.com.tw/upload/images/20190510/20112573DJSqco4JVl.png
所以master分支確實有套用feature分支的commit狀態。

切換到feature分支,它的commit歷程不受影響。
https://ithelp.ithome.com.tw/upload/images/20190510/20112573KrfDJ2cm0b.png

feature分支線圖,完全對應Git Bash的commit歷程。
https://ithelp.ithome.com.tw/upload/images/20190519/20112573xW8FQA14BM.png

tslint.json又出現了,因為feature分支並沒有刪除tslint.json。
https://ithelp.ithome.com.tw/upload/images/20190510/20112573fPs8CZduRK.png
這次的合併,feature確實沒有受到影響。

將feature合併到master之後,我們還是可以在feature分支繼續開發,不會影響master。

在feature新增檔案,寫入內容。
https://ithelp.ithome.com.tw/upload/images/20190510/20112573ZdtCn5hjy1.png
commit
https://ithelp.ithome.com.tw/upload/images/20190510/20112573Sz5k42iEtH.png

線圖顯示,feature分支的版本領先master分支。
https://ithelp.ithome.com.tw/upload/images/20190519/20112573OUIlAS22uR.png

新手容易犯的錯

有的新手看到feature分支有新版本,便會把master分支給合併到feature分支。
結果線圖變這樣。
https://ithelp.ithome.com.tw/upload/images/20190510/201125733xyzsZVnfe.png
是不是覺得線圖有點亂了?

這邊犯的錯就是,一開始是master分支合併feature分支,
接下來,feature分支又合併master分支,
這樣互相合併的結果,導致線圖的混亂。

正確合併的觀念

先確定主要分支,之後其他分支有任何變更,一律由主要分支去合併。
從頭到尾都是單方向進行,由主要分支去合併,而不是反過來合併主要分支。

回復到合併前的狀態

指令 git reset --hard ORIG_HEAD
https://ithelp.ithome.com.tw/upload/images/20190510/20112573Dj5AcKuY6r.png

回復線圖。
https://ithelp.ithome.com.tw/upload/images/20190519/20112573OUIlAS22uR.png

執行正確合併

切換到master,再合併feature。
https://ithelp.ithome.com.tw/upload/images/20190510/20112573nTWjiTwCE7.png

線圖變得明瞭許多。
https://ithelp.ithome.com.tw/upload/images/20190510/201125732eRz49bFc3.png

刪除分支,繼續開發

此時master分支已經是最新版本了,feature分支可以選擇刪除,再從master分支另開分支繼續開發。
https://ithelp.ithome.com.tw/upload/images/20190510/20112573enYWyq9BqI.png
由於feature分支已經commit了,所以-d可以順利刪除。

目前只剩master分支。
https://ithelp.ithome.com.tw/upload/images/20190510/20112573r4j3T7QWv2.png

以上就是分支合併必須注意的事項。

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


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

尚未有邦友留言

立即登入留言