git合併分支上指定的commit

merge 可以勝任日常大部分的合併需求。但也會遇到某些特殊的狀況,例如正在開發一個新的功能,線上說有一個緊急的bug要修復。bug修好了但並不像把仍在開發的新功能代碼也提交到線上去。這時候也許想要一個只合並指定某些 commit 的功能。git

假設分支結構以下:.net

dd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 [master]
                  \
                76cada-62ecb3-b886a0 [feature]ci

再假設 62ecb3 的提交修復了bug,這時候能夠用cherry pick 合併單個 commit開發

具體操做:get

git checkout masterit

git cherry-pick 62ecb3ast

就這麼簡單。62ecb3 已經應用在 master 上了(做爲一個新的commit)。bug

 

cherry pick 連續多個commitco

cherry pick 雖好,但一次只能合併一個commit。合併多個就要用到 rebase 了。再次假設想要把 76cada 和 62ecb3 合併到 master 上。new

操做:

git checkout -b newbranch 62ecb3

git rebase —onto master 76cada^

76cada^ 表示從 76cada 的 commit 開始合併(做爲新的commit)。這樣就完成了 76cada 到 62ecb3 合併到 master。

 

參考 https://ariejan.net/2010/06/10/cherry-picking-specific-commits-from-another-branch/

關於 cherry pick https://git-scm.com/docs/git-cherry-pick

關於 rebase http://git-scm.com/book/en/v2/Distributed-Git-Maintaining-a-Project#Rebasing-and-Cherry-Picking-Workflows

相關文章
相關標籤/搜索