git cherry-pick git cherry-pick
可以了解為”挑揀”送出,它會擷取某一個分支的單筆送出,并作為一個新的送出引入到你目前分支上。 當我們需要在本地合入其他分支的送出時,如果我們不想對整個分支進行合并,而是隻想将某一次送出合入到本地目前分支上,那麼就要使用
了。
用法
git cherry-pick [<options>] <commit-ish>...
常用options:
--quit 退出目前的chery-pick序列
--continue 繼續目前的chery-pick序列
--abort 取消目前的chery-pick序列,恢複目前分支
-n, --no-commit 不自動送出
-e, --edit 編輯送出資訊
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
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
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
現在,我想要将branch2分支上的第一次送出内容合入到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
繼續。
現在檢視送出資訊:
$ git log --oneline -3
790f431 [Description]:branch2 commit 2
20fe2f9 commit second
c51adbe commit first
branch2分支上的第二次送出成功合入到了branch1分支上。
以上就是
git cherry-pick
的基本用法,如果沒有出現沖突,該指令将自動送出。
git cherry-pick -n
如果不想
git cherry-pick
自動進行送出,則加參數
-n
即可。比如将branch2分支上的第三次送出内容合入到branch1分支上:
$ git cherry-pick 23d9422
[branch1 2c67715] [Description]:branch2 commit 3
Date: Fri Jul 13 18:37:05 2018 +0800
1 file changed, 1 insertion(+)
$
檢視送出log,它自動合入了branch1分支:
$ git log --oneline -3
2c67715 [Description]:branch2 commit 3
f8bc5db [Description]:branch2 commit 2
20fe2f9 commit second
如果不想進行自動合入,則使用
git cherry-pick -n
:
# 回退上次送出,再此進行cherry-pick
$ git reset --hard HEAD~
HEAD is now at f8bc5db [Description]:branch2 commit 2
$ git cherry-pick -n 23d9422
$ git status
On branch branch1
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: only-for-branch2.txt
$
這時通過
git status
檢視,發現已将branch2的送出擷取但是沒有合入。
git cherry-pick -e
如果想要在
cherr-pick
後重新編輯送出資訊,則使用
git cherry-pick -e
指令,比如我們還是要将branch2分支上的第三次送出内容合入到branch1分支上,但是需要修改送出資訊:
$ git cherry-pick -e 23d9422
1 [Description]:branch2 commit 3
2 #
3 # It looks like you may be committing a cherry-pick.
4 # If this is not correct, please remove the file
5 # .git/CHERRY_PICK_HEAD
6 # and try again.
git cherry-pick –continue, –abort,–quit
當使用
git cherry-pick
發生沖突後,将會出現如下資訊:
$ git cherry-pick 23d9422
error: could not apply 23d9422... [Description]:branch2 commit 3
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 add .
将檔案标記為已解決,然後可以使用
git cherry-pick --continue
指令,繼續進行
cherry-pick
操作。
如果要中斷這次
cherry-pick
,則使用
git cherry-pick --quit
,這種情況下目前分支中未沖突的内容狀态将為
modified
,
如果要取消這次
cherry-pick
git cherry-pick --abort
,這種情況下目前分支恢複到
cherry-pick
前的狀态,沒有改變。
git cherry-pick < branchname >
如果在
git cherry-pick
後加一個分支名,則表示将該分支頂端送出進cherry-pick,如:
$ git cherry-pick master
git cherry-pick ..< branchname >
git cherry-pick ^HEAD < branchname >
以上兩個指令作用相同,表示應用所有送出引入的更改,這些送出是branchname的祖先但不是HEAD的祖先,比如,現在我的倉庫中有三個分支,其送出曆史如下圖:
C<---D<---E branch2
/
master A<---B
\
F<---G<---H branch3
|
HEAD
如果我使用
git cherry-pick ..branch2
或者
git cherry-pick ^HEAD branch2
,那麼會将屬于branch2的祖先但不屬于branch3的祖先的所有送出引入到目前分支branch3上,并生成新的送出,執行指令如下:
$ git cherry-pick ..branch2
[branch3 c95d8b0] [Description]:branch2 add only-for-branch2
Date: Fri Jul 13 20:34:40 2018 +0800
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 only-for-branch2
[branch3 7199a67] [Description]:branch2 modify for only-for-branch2--1
Date: Fri Jul 13 20:38:35 2018 +0800
1 file changed, 1 insertion(+)
[branch3 eb8ab62] [Description]:branch2 modify for only-for-branch2--2
Date: Fri Jul 13 20:39:09 2018 +0800
1 file changed, 1 insertion(+)
執行後的送出曆史如下:
C<---D<---E branch2
/
master A<---B
\
F<---G<---H<---C'<---D'<---E' branch3
|
HEAD
常見問題
1.The previous cherry-pick is now empty, possibly due to conflict resolution.
原因:
在
cherry-pick
時出現沖突,解決沖突後本地分支中内容和
cherry-pick
之前相比沒有改變,是以當在以後的步驟中繼續
git cherry-pick
或執行其他指令時,由于此時還處于上次
cherry-pick
,都會提示該資訊,表示可能是由于解決沖突造成上一次cherry-pick内容是空的。
解決方案:
- 1.執行
取消上次操作。git cherry-pick --abort
- 2.執行
,表示允許空送出。git commit --allow-empty
2.fatal: You are in the middle of a cherry-pick – cannot amend.
cherry-pick
時出現沖突,沒有解決沖突就執行
git commit --amend
指令,進而會提示該資訊。
首先在
git commit --amend
之前解決沖突,并完成這次
cherry-pick
:
$ git add .
$ git cherry-pick --continue
git cherry-pick可以選擇某一個分支中的一個或幾個commit(s)來進行操作。例如,假設我們有個穩定版本的分支,叫v2.0,另外還有個開發版本的分支v3.0,我們不能直接把兩個分支合并,這樣會導緻穩定版本混亂,但是又想增加一個v3.0中的功能到v2.0中,這裡就可以使用cherry-pick了。
就是對已經存在的commit 進行 再次送出;
簡單用法:
git cherry-pick <commit id>
注意:當執行完 cherry-pick 以後,将會 生成一個新的送出;這個新的送出的哈希值和原來的不同,但辨別名 一樣;
例如:
$ git checkout old_cc
$ git cherry-pick 38361a68 # 這個 38361a68 号碼,位于:
$ git logcommit 38361a68138140827b31b72f8bbfd88b3705d77aAuthor: Siwei Shen <[email protected]>Date: Sat Dec 10 00:09:44 2011 +0800
1. 如果順利,就會正常送出。結果:
Finished one cherry-pick.# On branch old_cc# Your branch is ahead of 'origin/old_cc' by 3 commits.
2. 如果在cherry-pick 的過程中出現了沖突
Automatic cherry-pick failed. After resolving the conflicts,mark the corrected paths with 'git add <paths>' or 'git rm <paths>'and commit the result with:
git commit -c 15a2b6c61927e5aed6718de89ad9dafba939a90b
就跟普通的沖突一樣,手工解決:
2.1 $ git status # 看哪些檔案出現沖突
both modified: app/models/user.rb
2.2 $ vim app/models/user.rb # 手動解決它。
2.3 $ git add app/models/user.rb
2.4 git commit -c <新的commit号碼>