获取当前分支的最后 N 次提交并将它们应用到另一个分支

Take last N commits of the current branch and apply them to another branch

这是我的场景:

我想从这里开始:

                       (branch) feature1: f1 > f2 > f3 > f4
                      /
(branch) master: A > B > C > D

为此:

                       (branch) feature1:  f1 > f2 > f3 > f4
                      /
(branch) master: A > B > C > D > f3 > f4

您可以挑选选择性提交到 master 上:

https://git-scm.com/docs/git-cherry-pick

您可以通过 运行

观察您提交的最后哈希值

git log -N 其中 N 是您要查看的提交哈希数。

然后你可以做

git cherry-pick {firstHash}^..{lastHash} 其中 {firstHash}{secondHash} 是你挑选樱桃的起点和终点,它就像闭区间 [firstHash,lastHash] ,所以中间的所有提交都会也被带走。

注意: firstHash 必须在 lastHash 之前出现,原因很明显。

如果你不希望它们被提交,你只想要它们的内容,你可以这样做

git cherry-pick {firstHash}^..{lastHash} --no-commit

在你的情况下,如果我们认为 (f1,f2..) 是提交哈希,这应该可以进行练习:

git checkout master
git cherry-pick f3^..f4

如果有任何合并冲突,您将不得不解决它们并调用 git cherry-pick --continue(有时 git bash 固执己见,但它没有向您展示)到能够继续下一次提交。

在任何情况下,您都应该查看 @Erhan 提到的 cherry pick 的文档 - https://git-scm.com/docs/git-cherry-pick