当涉及到一系列提交时,挑选樱桃是 是不切实际的。
作为Keith Kim 的mentioned below,Git 1.7.2+ 引入了挑选一系列提交的能力(但您仍然需要注意consequence of cherry-picking for future merge)
git cherry-pick" 学会了选择一系列提交
(例如“cherry-pick A..B”和“cherry-pick --stdin”),“git revert”也是如此;不过,这些不支持更好的排序控件“rebase [-i]”。
damiancomments 并警告我们:
在“cherry-pick A..B”表单中,A 应早于 B。
如果顺序错误,命令将静默失败强>。
如果您想选择 范围 B 到 D(包括 B),那将是 B^..D(而不是 B..D) .
请参阅“Git create branch from range of previous commits?”作为插图。
正如Jubobs 提到in the comments:
这假设B 不是根提交;否则会收到“unknown revision”错误。
注意:从 Git 2.9.x/2.10(2016 年第三季度)开始,您可以直接在孤立分支(空头)上挑选一系列提交:请参阅“How to make existing branch an orphan in git”。
原始答案(2010 年 1 月)
rebase --onto 会更好,您可以在集成分支上重放给定的提交范围,如 Charles Bailey described here。
(另外,在git rebase man page 中查找“这是如何将基于一个分支的主题分支移植到另一个分支”,以查看git rebase --onto 的实际示例)
如果您当前的分支是集成:
# Checkout a new temporary branch at the current location
git checkout -b tmp
# Move the integration branch to the head of the new patchset
git branch -f integration last_SHA-1_of_working_branch_range
# Rebase the patchset onto tmp, the old location of integration
git rebase --onto tmp first_SHA-1_of_working_branch_range~1 integration
这将重播以下之间的所有内容:
- 在
first_SHA-1_of_working_branch_range 的父级之后(因此是~1):您要重播的第一个提交
- 直到“
integration”(指向您要重放的最后一次提交,来自working 分支)
到“tmp”(指向integration之前指向的位置)
如果在重放其中一个提交时出现任何冲突:
- 要么解决它并运行“
git rebase --continue”。
- 或跳过此补丁,改为运行“
git rebase --skip”
- 或使用“
git rebase --abort”取消所有内容(并将integration 分支放回tmp 分支)
在那之后rebase --onto,integration 将回到集成分支的最后一次提交(即“tmp”分支+所有重放的提交)
使用cherry-picking 或rebase --onto,不要忘记它会对后续合并产生影响,如described here。
一个纯粹的“cherry-pick”解决方案是discussed here,它会涉及到:
如果您想使用补丁方法,那么“git format-patch|git am”和“git cherry”是您的选择。
目前,git cherry-pick 只接受一个提交,但如果你想选择B 到D 的范围,在 git 术语中是B^..D,所以
git rev-list --reverse --topo-order B^..D | while read rev
do
git cherry-pick $rev || break
done
但无论如何,当您需要“重播”一系列提交时,“重播”一词应该会促使您使用 Git 的“rebase”功能。