2-8 情景五:已有的功能,需要摘取其中的提交cherry-pick
场景描述
某个分支上有几个有价值的提交,你想把这些提交"摘取"到当前分支,但不想合并整个分支。git cherry-pick 正好解决这个问题。
使用方法
# 查看目标分支的提交记录
git log --oneline feature/other-branch
# 摘取单个提交
git cherry-pick a1b2c3f
# 摘取多个提交
git cherry-pick a1b2c3f d4e5f6g
# 摘取一个范围的提交(不包含起始)
git cherry-pick start-hash..end-hash
bash
处理冲突
如果摘取的提交与当前分支有冲突:
# 解决冲突后
git add .
git cherry-pick --continue
# 放弃摘取
git cherry-pick --abort
bash
注意事项
- cherry-pick 会创建新的提交(新的哈希值),不是移动原提交
- 避免对已经 cherry-pick 过的提交再次 cherry-pick,会导致重复
- 如果需要摘取大量提交,考虑使用 merge 或 rebase 替代
参考资源
↑