適用場景:
git cherry-pick可以理解為”挑揀”提交,它會獲取某一個分支的單筆提交,并作為一個新的提交引入到你當前分支上。 當我們需要在本地合入其他分支的提交時,如果我們不想對整個分支進行合并,而是只想將某一次提交合入到本地當前分支上,那么就要使用git cherry-pick了。
用法
git cherry-pick [<options>] <commit-ish>...
常用options:
? ? --quit? ? ? ? ? ? ? ? 退出當前的chery-pick序列
? ? --continue? ? ? ? ? ? 繼續(xù)當前的chery-pick序列
? ? --abort? ? ? ? ? ? ? 取消當前的chery-pick序列,恢復當前分支
? ? -n, --no-commit? ? ? 不自動提交
? ? -e, --edit? ? ? ? ? ? 編輯提交信息
git cherry-pick commitid
在本地倉庫中,有兩個分支:branch1和branch2,我們先來查看各個分支的提交:
# 切換到branch2分支
$ git checkout branch2
Switched to branch 'branch2'
# 查看最近三次提交
$ git log --oneline -3
23d9422 [Description]:branch2 commit 3
2555c6e [Description]:branch2 commit 2
b82ba0f [Description]:branch2 commit 1
# 切換到branch1分支
$ git checkout branch1
Switched to branch 'branch1'
# 查看最近三次提交
$ git log --oneline -3
20fe2f9 commit second
c51adbe commit first
ae2bd14 commit 3th
現(xiàn)在,我想要將branch2分支上的第一次提交內(nèi)容合入到branch1分支上,則可以使用git cherry-pick命令:
$ git cherry-pick 2555c6e
error: could not apply 2555c6e... [Description]:branch2 commit 2
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
當cherry-pick時,沒有成功自動提交,這說明存在沖突,因此首先需要解決沖突,解決沖突后需要git commit手動進行提交:
$ git commit
[branch1 790f431] [Description]:branch2 commit 2
Date: Fri Jul 13 18:36:44 2018 +0800
1 file changed, 1 insertion(+)
create mode 100644 only-for-branch2.txt
或者git add .后直接使用git cherry-pick --continue繼續(xù)。
現(xiàn)在查看提交信息:
$ git log --oneline -3
790f431 [Description]:branch2 commit 2
20fe2f9 commit second
c51adbe commit first
branch2分支上的第二次提交成功合入到了branch1分支上。
以上就是git cherry-pick的基本用法,如果沒有出現(xiàn)沖突,該命令將自動提交。