1、VS
git cherry-pick:将一个分支上的某些提交应用到另一个分支
git merge:需要另一分支的所有代码变动
image.png
2、git cherry-pick
git cherry-pick <commit-hash> # 指定提交应用于当前分支;当前分支产生一个新的提交
git cherry-pick <commit-hash> <commit-hash2> …… # 多个提交应用于当前分支
git cherry-pick <branch_name>
git cherry-pick <hash1>..<Hash2> # hash1-hash2多个提交(不包括hash1)
git cherry-pick <hash1>^..<Hash2> # hash1-hash2多个提交(包括hash1)
常见选项
-
-e,--edit# 打开vim,编辑提交信息 -
-n,--no-commit# 只更新工作区和暂存区,不产生新的提交 -
-x# 追加备注信息 -
-s,--signoff# 备注信息后追加操作者签名 -
-m parent_number# 原始提交是一个合并节点,来自于两个分支的合并,那么Cherry pick默认失败。当前则指定哪一个分支的变动,parent_number从1开始的整数,代表原始提交的父分支编号(1-接受变动的分支,2-作为变动来源的分支)
29447@GW64 /d/myProject (main)
$ git log --graph -5 --oneline
* 9ffd801 (HEAD -> main) [main|merging]fix conflicts in readme.md
|\
| * 5e5e3ef (dev) [dev]rename file2.txt to cheery-pick-file
| * 0488ebb [dev]修改readme.md,V14
| * 8d32ebd 【dev branch】v12 修改readme 第12次
| * e0ab264 【dev branch】v11 修改readme 第11次
29447@GW64 /d/myProject (main)
$ git checkout temp_branch
Switched to branch 'temp_branch'
29447@GW64 /d/myProject (temp_branch)
$ git cherry-pick 5e5e3ef # 指定提交
[temp_branch 79d1e28] [dev]rename file2.txt to cheery-pick-file
Date: Sun Jun 22 09:14:44 2025 +0800
1 file changed, 0 insertions(+), 0 deletions(-)
rename file2.txt => cheerry-pick-file (100%)
29447@GW64 /d/myProject (temp_branch)
$ git log --oneline --graph -5
* 79d1e28 (HEAD -> temp_branch) [dev]rename file2.txt to cheery-pick-file # 生成新提交
* 1d40dca [temp_branch]修改license,v1.0
* 8f19424 temp_branch:提交修改
* d54c16b temp_branch:file1.txt重命名为readme.md
* 7ca3425 temp_branch:新增license,修改file1.tx
29447@GW64 /d/myProject (dev)
$ git log --graph --oneline -5
* 040eb2a (HEAD -> dev) [dev]add dev3
* 6c00698 [dev]add dev2
* 0293445 [dev]add dev1
* 5e5e3ef [dev]rename file2.txt to cheery-pick-file
* 0488ebb [dev]修改readme.md,V14
29447@GW64 /d/myProject (dev)
$ git checkout -
M cheerry-pick-file
Switched to branch 'temp_branch'
29447@GW64 /d/myProject (temp_branch)
$ git log --graph --oneline -5
* 79d1e28 (HEAD -> temp_branch) [dev]rename file2.txt to cheery-pick-file
* 1d40dca [temp_branch]修改license,v1.0
* 8f19424 temp_branch:提交修改
* d54c16b temp_branch:file1.txt重命名为readme.md
* 7ca3425 temp_branch:新增license,修改file1.tx
29447@GW64 /d/myProject (temp_branch)
$ git cherry-pick 0293445..040eb2a # 未包括hash1
[temp_branch ebc86fa] [dev]add dev2
Date: Sun Jun 22 12:39:00 2025 +0800
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 dev2
[temp_branch 856427b] [dev]add dev3
Date: Sun Jun 22 12:39:07 2025 +0800
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 dev3
29447@GW64 /d/myProject (temp_branch)
$ git log --graph --oneline -5
* 856427b (HEAD -> temp_branch) [dev]add dev3 # 新增提交
* ebc86fa [dev]add dev2 # 新增提交
* 79d1e28 [dev]rename file2.txt to cheery-pick-file
* 1d40dca [temp_branch]修改license,v1.0
* 8f19424 temp_branch:提交修改
3、冲突
git cherry-pick --continue # 代码冲突解决后,继续执行
git cherry-pick --abort # 放弃合并,回到操作前的样子
git cherry-pick --quit # 退出cherry-pick,但不回到操作前的样子
29447@GW64 /d/myProject (dev)
$ git log --oneline -5
59de60b (HEAD -> dev) [dev]update dev3
bf2590a [dev]update dev2
1fd0f26 [dev]update dev1
040eb2a [dev]add dev3
6c00698 [dev]add dev2
29447@GW64 /d/myProject (dev)
$ git checkout -
M cheerry-pick-file
Switched to branch 'temp_branch'
29447@GW64 /d/myProject (temp_branch)
$ git cherry-pick 59de60b^..1fd0f26 # 提交早的是hash1,否则报错
error: empty commit set passed
fatal: cherry-pick failed
29447@GW64 /d/myProject (temp_branch)
$ git cherry-pick 1fd0f26^..59de60b
error: your local changes would be overwritten by cherry-pick.
hint: commit your changes or stash them to proceed.
fatal: cherry-pick failed # 本次提交可能影响本地尚未提交的更改
29447@GW64 /d/myProject (temp_branch|CHERRY-PICKING)
$ git status -s
M cheerry-pick-file
29447@GW64 /d/myProject (temp_branch|CHERRY-PICKING)
$ git commit -m "[temp_branch]update cheery-pick-file"
[temp_branch 95c7f5e] [temp_branch]update cheery-pick-file
1 file changed, 2 deletions(-)
29447@GW64 /d/myProject (temp_branch|CHERRY-PICKING)
$ git status -s
29447@GW64 /d/myProject (temp_branch|CHERRY-PICKING)
$ git cherry-pick --continue # 冲突解决后继续
[temp_branch 5ac650c] [dev]update dev2
Date: Sun Jun 22 13:07:52 2025 +0800
1 file changed, 1 insertion(+)
[temp_branch c926e9a] [dev]update dev3
Date: Sun Jun 22 13:07:59 2025 +0800
1 file changed, 1 insertion(+)
29447@GW64 /d/myProject (temp_branch)
$ git log --oneline -5
c926e9a (HEAD -> temp_branch) [dev]update dev3 # 提交2
5ac650c [dev]update dev2 # 提交1
95c7f5e [temp_branch]update cheery-pick-file
856427b [dev]add dev3
ebc86fa [dev]add dev2
29447@GW64 /d/myProject (temp_branch)
$ git cherry-pick 1fd0f26
CONFLICT (modify/delete): dev1 deleted in HEAD and modified in 1fd0f26 ([dev]update dev1). Version 1fd0f26 ([dev]update dev1) of dev1 left in tree.
error: could not apply 1fd0f26... [dev]update dev1 # 当前分支没有cherry-pick新增dev1的提交,所以当前没有该文件;故当前分支视为dev1删除,与1fd0f26提交的修改dev1冲突
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
29447@GW64 /d/myProject (temp_branch|CHERRY-PICKING)
$ git cherry-pick --abort
29447@GW64 /d/myProject (temp_branch)
$
29447@GW64 /d/myProject (temp_branch)
$ git cherry-pick 0293445
[temp_branch fce235a] [dev]add dev1
Date: Sun Jun 22 12:38:51 2025 +0800
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 dev1
29447@GW64 /d/myProject (temp_branch)
$ git cherry-pick 1fd0f26 # 新cherry-pick新增dev1的提交hash,然后再执行该命令,成功
[temp_branch c1cfecd] [dev]update dev1
Date: Sun Jun 22 13:07:45 2025 +0800
1 file changed, 1 insertion(+)
29447@GW64 /d/myProject (temp_branch)
$
4、转移到另一个代码库
step1:git remote add <remote_name> <remote_url> # 添加为远程仓库
step2:git fetch <remote_name> # 将远程代码抓取到本地
step3:git log <remote_name>/main # 获取哈希值
step4:git cherry-pick <hash> # 转移提交
29447@GW64 /d/myProject (temp_branch) # python_clone仓库
$ cd ../github_rep/
29447@GW64 /d/github_rep (main) # python仓库
$ git remote add python_clone git@github.com:bai-cao/python_clone.git
29447@GW64 /d/github_rep (main)
$ git fetch python_clone
remote: Enumerating objects: 407, done.
remote: Counting objects: 100% (218/218), done.
remote: Compressing objects: 100% (189/189), done.
remote: Total 407 (delta 34), reused 207 (delta 26), pack-reused 189 (from 1)
Receiving objects: 100% (407/407), 1.66 MiB | 198.00 KiB/s, done.
Resolving deltas: 100% (100/100), done.
From github.com:bai-cao/python_clone
* [new branch] main -> python_clone/main
* [new tag] v1.0 -> v1.0
* [new tag] v2.0 -> v2.0
29447@GW64 /d/github_rep (main)
$ git log python_clone/main --oneline -10
612d754 (python_clone/main, python_clone/HEAD) main:Merge branch 'temp_branch'
8f19424 temp_branch:提交修改
d54c16b temp_branch:file1.txt重命名为readme.md
7ca3425 temp_branch:新增license,修改file1.tx
4ef9ef1 temp_branch:第3次修改
8ef85f0 temp_branch:modified file1.txt
4f5d635 temp_branch:添加file2.txt
6103fc6 temp_branch:add file1.txt
1f4064c temp_branch:delete html files&sources
5a9482b main:delete html files
29447@GW64 /d/github_rep (main)
$ git log --oneline -10
0e02d19 (HEAD -> main, origin/main, origin/HEAD) 新增Package
1d5fa89 Initial commit
29447@GW64 /d/github_rep (main)
$ git cherry-pick -n 6103fc6^..8f19424 # -n 工作区+暂存区,不提交
29447@GW64 /d/github_rep (main)
$ git status -s
M README.md
A file2.txt
A license
A readme.md









网友评论