如何仅通过一次提交将一系列更改重新定位到另一个分支?

how can I rebase a range of changes into another branch with just one commit?

我有如下分支:

             L--M   <-- master
            /
...--A--B--C
            \
             D--E--F--G--H-I--J--K   <-- dev(HEAD)

我想做的是通过一次提交将 EFGHIJ 复制到 master,如下所示:

             L--M--N   <-- master
            /
...--A--B--C
            \
             D--E--F--G--H-I--J--K   <-- dev(HEAD)

N 应包含 EFGHIJ 更改。

我可以使用 rebase --onto 命令将更改变基到 master

git rebase --onto master E~1 J

如果我使用 git 推送,master 中将存在 EFGHIJ 更改。 也许我需要使用 git rebase -i 命令将它们更改为一个,但是,问题是,我看不到 [=31= 应用的先前更改的更改历史记录] rebase --onto!

我怎样才能一次提交它们?

单条命令即可(rebase -i),但需要人工干预:需要编辑rebase的todo列表:

git rebase -i --onto M E^ J
# then in vim: :2,$s/^pick/fixup/
# save and close vim, voilà

如果您需要 specific/different 提交消息,请将第一行中的 pick 更改为 reword 并相应地输入您的消息。


您对单个命令的要求从何而来?让我再想想,diff+apply 的组合可能是可行的(但话又说回来,你有一个管道,它实际上是两个命令)

编辑:diff+apply 有效,但您随后需要另一个命令(提交),所以我认为使用 rebase 是最简单和最安全的选择。为了完整起见:

git checkout M
git diff E^ J | git apply
git commit -m 'Combination of commits E^..J'

git rebase --onto master E~1 J之后运行

git reset --soft M
git commit -m 'changes from E F G H I and J'

完成!