文章目录
- 前言
- git
- 资源
前言
本地 Git 仓库有两个分支,分别为 main 和 dev,dev 是 main 在 hash 为 a2
的时候创建的开发分支:
现在需要将 dev 分支中 hash 为 b1
的 commit 单独合并到分支 main 去:
这种将 dev 中部分特定 commit 合并到其他分支的操作在 git
中的命令是 cherry-pick
🍒
git
准备好初始环境:
> git branch
dev
* main
>
> git log
commit 77187baebd460f50dd879c03b1932f13d13171cc (HEAD -> main)
Author: xianzhan <email@qq.com>
Date: Wed Nov 29 22:56:03 2023 +0800
a4
commit 859e40cf0b9ace1b543ca7a88e62f7820cfa978d
Author: xianzhan <email@qq.com>
Date: Wed Nov 29 22:55:28 2023 +0800
a3
commit de4d2ecc931a45da3f02c2a84d253f50d142f6e2
Author: xianzhan <email@qq.com>
Date: Wed Nov 29 22:51:17 2023 +0800
a2
commit 276c4e3159700a74ee33eb75903dd85a192460c5
Author: xianzhan <email@qq.com>
Date: Wed Nov 29 22:50:37 2023 +0800
a1
>
> git checkout dev
Switched to branch 'dev'
>
> git log
commit bf7fa7184ba7407f4d55ede26c5de8addf115498 (HEAD -> dev)
Author: xianzhan <email@qq.com>
Date: Wed Nov 29 22:54:46 2023 +0800
b2
commit adcdccd49e86b15d18d2bae4fe45cf077b1f087c
Author: xianzhan <email@qq.com>
Date: Wed Nov 29 22:54:13 2023 +0800
b1
commit de4d2ecc931a45da3f02c2a84d253f50d142f6e2
Author: xianzhan <email@qq.com>
Date: Wed Nov 29 22:51:17 2023 +0800
a2
commit 276c4e3159700a74ee33eb75903dd85a192460c5
Author: xianzhan <email@qq.com>
Date: Wed Nov 29 22:50:37 2023 +0800
a1
OK,环境准备好之后就是最重要的操作了:
> git checkout main
Switched to branch 'main'
>
# 分支 dev 提交 b1 对应的 hash
> git cherry-pick 'adcdccd49e86b15d18d2bae4fe45cf077b1f087c'
Auto-merging cherry.txt
CONFLICT (content): Merge conflict in cherry.txt
error: could not apply adcdccd... b1
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".
有上面的一大串文字是因为在合并时文件有冲突了,只要我们使用 VS Code(或者其他编辑器)解决就好:
点击右下角的 Resolve in Merge Editor
解决冲突地方
确定无误后点击 Complete Merge
资源
Git - git-cherry-pick Documentation