您现在的位置是:首页 >技术杂谈 >Git常用命令cherry-pick网站首页技术杂谈

Git常用命令cherry-pick

zsx_yiyiyi 2024-08-12 12:01:03
简介Git常用命令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
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。