我如何使用 rebase -i 以便一个提交包含另一个提交的更改?

How can I use rebase -i so that one commit contains changes of another one?

考虑这种情况。我有 5 个提交 A-B-C-D-E。我想使用 rebase -i 以便提交 B 将具有提交 D 的所有更改。我该怎么做?

git rebase -i A

编辑器弹出内容:

pick B
pick C
pick D
pick E

编辑内容:

pick B
squash D
pick C
pick E

保存并退出。

你可以简单地做 git rebase -i HEAD~4 然后你会看到

pick ae2c481 B
pick 2b1007f C
pick bcda8c5 D
pick 8591cea E

# Rebase 69f1cd1..8591cea onto 69f1cd1 (4 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

然后

pick ae2c481 B
squash bcda8c5 D
pick 2b1007f C
pick 8591cea E