Git常用命令cherry-pick

文章介绍了Git的cherry-pick命令,用于将特定提交应用到其他分支,详细阐述了如何选择并合并单个或多个提交,以及如何处理在cherry-pick过程中出现的冲突。通过示例展示了如何恢复被撤销的提交,以及在冲突解决后如何继续或取消cherry-pick操作。

Git常用命令cherry-pick

将指定的提交应用于其他分支,可以用于恢复不小心撤销(revert/reset)的提交。

对于多分支的代码库,将代码从一个分支转移到另一个分支是常见需求。

这时分两种情况。一种情况是,你需要另一个分支的所有代码变动,那么就采用合并 git merge 。另一种情

况是,你只需要部分代码变动(某几个提交),这时可以采用 cherry pick

1、cherry-pick使用

git cherry-pick 命令的参数,不一定是提交的 commit 值,分支名也是可以的,表示转移该分支的最新提交。

# 选择commit,合并进当前分支,会在当前分支产生一个新的提交
# 去合并某分支的某一次提交记录
$ git cherry-pick commit
# 合并分支的某几次提交记录
$ git cherry-pick commit1 commit2 ...
# 合并提交记录commit1到commit2之间的所有的提交记录,但不包含commit1的这次提交记录
$ git cherry-pick commit1..commit2
# 合并提交记录commit1到commit2之间的所有的提交记录,包含commit1的这次提交记录
$ git cherry-pick commit1^..commit2
# 合并该分支上最新的一次提交记录
$ git cherry-pick branch_name

例子:

$ git branch
* branch_a
  master
# branch_a
$ git log --oneline
ddbfc0b (HEAD -> branch_a, origin/branch_a) branch_a | update a.txt | add new.txt
87d5c63 add f.txt
47e8b59 add e.txt
c0547da add d.txt
9c173bb add c.txt
8c4a625 add b.txt
8e58180 add a.txt
# master
git log --oneline
d735ee3 (HEAD -> master, origin/master, origin/branch_d, origin/HEAD) branch_d | update a.txt | update b.txt | update e.txt
8cb57f6 (origin/branch_c) branch_c | update a.txt | delete e.txt
5b05cb6 (origin/branch_b) branch_b | update a.txt | add new.txt
ddbfc0b (origin/branch_a, branch_a) branch_a | update a.txt | add new.txt
87d5c63 add f.txt
47e8b59 add e.txt
c0547da add d.txt
9c173bb add c.txt
8c4a625 add b.txt
8e58180 add a.txt
# 切换到branch_a分支
$ git checkout branch_a
Switched to branch 'branch_a'
Your branch is up-to-date with 'origin/branch_a'.

# cherry-pick commit
$ git cherry-pick 5b05cb6
[branch_a 1b4e841] branch_b | update a.txt | add new.txt
 Date: Sat May 27 13:14:01 2023 +0800
 2 files changed, 2 insertions(+)
 
git log --oneline
1b4e841 (HEAD -> branch_a) branch_b | update a.txt | add new.txt
ddbfc0b (origin/branch_a) branch_a | update a.txt | add new.txt
87d5c63 add f.txt
47e8b59 add e.txt
c0547da add d.txt
9c173bb add c.txt
8c4a625 add b.txt
8e58180 add a.txt

$ cat a.txt
branch_a
branch_b
# git cherry-pick commit范围
$ git cherry-pick 5b05cb6..d735ee3
[branch_a a50b7e5] branch_c | update a.txt | delete e.txt
 Date: Sat May 27 13:31:13 2023 +0800
 2 files changed, 2 insertions(+), 1 deletion(-)
 delete mode 100644 e.txt
[branch_a fb8bbfc] branch_d | update a.txt | update b.txt | update e.txt
 Date: Sat May 27 17:04:00 2023 +0800
 3 files changed, 3 insertions(+), 1 deletion(-)
 delete mode 100644 b.txt
 create mode 100644 e.txt

$ git log --oneline
fb8bbfc (HEAD -> branch_a) branch_d | update a.txt | update b.txt | update e.txt
a50b7e5 branch_c | update a.txt | delete e.txt
1b4e841 branch_b | update a.txt | add new.txt
ddbfc0b (origin/branch_a) branch_a | update a.txt | add new.txt
87d5c63 add f.txt
47e8b59 add e.txt
c0547da add d.txt
9c173bb add c.txt
8c4a625 add b.txt
8e58180 add a.txt

$ cat a.txt
branch_a
branch_b
branch_c
branch_d

2、cherry-pick冲突

再重新执行:

$ git cherry-pick 5b05cb6
error: could not apply 5b05cb6... branch_b | update a.txt | add new.txt
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'

# 查看状态
$ git status
On branch branch_a
Your branch is ahead of 'origin/branch_a' by 3 commits.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 5b05cb6.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")

# 出现冲突
$ cat a.txt
branch_a
<<<<<<< HEAD
branch_b
branch_c
branch_d
=======
branch_b
>>>>>>> 5b05cb6... branch_b | update a.txt | add new.txt

那么 cherry-pick 时出现冲突该如何解决。

2.1 修复冲突后执行git cherry-pick --continue

# 解决冲突
$ cat a.txt
branch_a
branch_b

$ git cherry-pick --continue
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
U       a.txt

$ git add a.txt

$ git cherry-pick --continue
[branch_a 04d5f2f] branch_b | update a.txt | add new.txt
 Date: Sat May 27 13:14:01 2023 +0800
 1 file changed, 1 insertion(+), 3 deletions(-)
 
$ git log --oneline
04d5f2f (HEAD -> branch_a) branch_b | update a.txt | add new.txt
fb8bbfc branch_d | update a.txt | update b.txt | update e.txt
a50b7e5 branch_c | update a.txt | delete e.txt
1b4e841 branch_b | update a.txt | add new.txt
ddbfc0b (origin/branch_a) branch_a | update a.txt | add new.txt
87d5c63 add f.txt
47e8b59 add e.txt
c0547da add d.txt
9c173bb add c.txt
8c4a625 add b.txt
8e58180 add a.txt

$ cat a.txt
branch_a
branch_b

2.2 放弃cherry-pick执行git cherry-pick --abort

如果不想继续 cherry-pick,那么可以取消 cherry-pick,这个时候可以恢复到 cherry-pick 之前的样子。

$ git cherry-pick --abort

$ git status
On branch branch_a
Your branch is ahead of 'origin/branch_a' by 3 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

$ git log --oneline
fb8bbfc (HEAD -> branch_a) branch_d | update a.txt | update b.txt | update e.txt
a50b7e5 branch_c | update a.txt | delete e.txt
1b4e841 branch_b | update a.txt | add new.txt
ddbfc0b (origin/branch_a) branch_a | update a.txt | add new.txt
87d5c63 add f.txt
47e8b59 add e.txt
c0547da add d.txt
9c173bb add c.txt
8c4a625 add b.txt
8e58180 add a.txt
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值