Git cherry-pick 引入某个提交(commit)

2019-04-01 0 By admin

git cherry-pick :Apply the changes introduced by some existing commits.
git cherry-pick [<options>] <commit-id>...
--quit 退出当前的chery-pick序列
--continue 继续当前的chery-pick序列
--abort 取消当前的chery-pick序列,恢复当前分支
-n, --no-commit 不自动提交
-e, --edit 编辑提交信息

一、git cherry-pick 使用示例

# 切换到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

$ 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


二、git cherry-pick -n

如果不想git cherry-pick自动进行提交,则加参数-n即可。
$ 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 cherry-pick -e

如果想要在cherr-pick后重新编辑提交信息,则使用git cherry-pick -e命令,比如我们还是要将branch2分支上的第三次提交内容合入到branch1分支上,但是需要修改提交信息:
$ git cherry-pick -e 23d9422
[Description]:branch2 commit 3
# It looks like you may be committing a cherry-pick.
# If this is not correct, please remove the file
# and try again.