我对 git 中的整个变基功能有点陌生。假设我做了以下提交:
A -> B -> C -> D
之后,我意识到它D包含一个依赖于添加的一些新代码的修复A,并且这些提交属于一起。我如何将A&D挤在一起,然后B独自C离开?
D
A
B
C
您可以git rebase --interactive在 B 之前运行和重新排序 D 并将 D 压缩到 A 中。
git rebase --interactive
Git 会打开一个编辑器,你会看到一个像这样的文件,例如:git rebase --interactive HEAD~4
git rebase --interactive HEAD~4
pick aaaaaaa Commit A pick bbbbbbb Commit B pick ccccccc Commit C pick ddddddd Commit D # Rebase aaaaaaa..ddddddd onto 1234567 (4 command(s)) # # 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 # # 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 aaaaaaa Commit A squash ddddddd Commit D pick bbbbbbb Commit B pick ccccccc Commit C
并且 git 现在会将 A 和 D 的更改合并到一个提交中,然后将 B 和 C 放在一起。当您不想保留 D 的提交消息时squash,您将使用fixup关键字而不是 。有关更多信息fixup,您可以查阅git rebase文档,或查看这个问题,其中有一些很好的答案。
squash
fixup
git rebase