0


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
# mastergit 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 2713: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 2713: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 2717: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

$ gitadd a.txt

$ git cherry-pick --continue
[branch_a 04d5f2f] branch_b | update a.txt |add new.txt
 Date: Sat May 2713:14:01 2023 +0800
 1file 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
标签: git

本文转载自: https://blog.csdn.net/qq_30614345/article/details/131036783
版权归原作者 242030 所有, 如有侵权,请联系我们删除。

“Git常用命令cherry-pick”的评论:

还没有评论