iT邦幫忙

1

Git cherry-pick

今天一個情境下為,我們在master創建一個有bug的文件,而後在創建一個分支(stable 1.x),此時此分支與master是同步的,而後我們在master在創建一個新的文件,此時很明顯我們的分支是落後master,且兩者都帶有個bug的文件,此時我們如果各別去修復會很麻煩。且修復後兩者會產生不一樣的commit,導致兩者前面部分並未同步,我們所希望的是能達到在不增加且破壞原本的commit前提下,能進行修改有bug的commit即可

https://ithelp.ithome.com.tw/upload/images/20200706/20126182yFieKViswm.png

而我們先至master分支將有bug的文件給處理(vim 文件名)好後,再利用git add * => git commit -m "bug fix by jojo" => git push origin master,此時遠端項目看到commit已增加,bug也已經修復,那此時我們也將修復好的bug套用到也有同樣bug的分支(stable 1.x),我們可以直接用merge,但這樣不太好,因為我們在master的bug創建之後還有其他的一些操作這樣都會merge到其分支,會多一些不必要的commit,所以其實我們只需要修復bug的這個commit。

首先在master git log,複製修復bug commit的hash value,而後切換到分支(stable 1.x),並輸入 git cherry-pick (commit hash value)後,在git log,發現已經多出一個修復bug的commit,而後我們git push origin stable 1.x 給push到遠端,去check也發現commit已經確實增加,而且沒有多其他的commit。

https://ithelp.ithome.com.tw/upload/images/20200706/20126182RIjuSrIwAl.png

https://ithelp.ithome.com.tw/upload/images/20200706/20126182L4Xm8TS7Zx.png


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

尚未有邦友留言

立即登入留言