如何将早期的Git提交合并为一个?

3
我在我的分支上有大约10个提交记录,假设它们的ID是1到10。我想把第4、5和6个提交合并在一起,但保留其余的提交不变。我找到的每个教程都是从当前HEAD开始,然后使用git rebase -i HEAD~(x amount)向后操作。但是,我如何从早期的提交开始重新设置,并指定一个范围,例如git rebase -i 6...4?谢谢。

1
教程之所以是那样的,是因为它们必须是那样的。Rebase 复制提交,保持原始提交不变(必须这样做;Git 一旦提交就无法更改)。因此,您必须重新复制所有“修补”后的提交,因为原始未复制的提交指向它们早期、原始、未修补的提交。请参见下面 Jared 的答案。 - torek
2个回答

5

只需进行交互式变基,将您想要压缩或修复的提交压缩或修复即可。

git rebase -i HEAD~10

pick a4461d3 
pick d998164 
pick 0a1f6e1 
f 310ba9d 
f 60b7e01
f 7baef60
pick bb9a551
pick badbad1
pick fd9a10c
pick 59ed66f

# Rebase e7e1369..60b7e01 onto fd9a10c (10 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
# 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

没想到我可以这样做。谢谢。 - thatDubstepSound

1
好的,@jaredr已经回答了你的问题。针对问题的第二部分,GIT没有提供这样的命令(git rebase -i 4..6),实际上也不应该有! 想象一下GIT将要执行多少操作才能开始提供这样的命令(它必须对某些提交执行操作并堆叠其他提交。正确合并会增加更多的复杂性。因此,它将此工作转移到用户本身,因为他/她更了解代码。) 此外,它还提供了类似于Git squash 的辅助机制,您可以在其中暂时丢弃提交并处理其他事情。

我有点明白你的意思。然而,在完全线性的提交历史中(假设没有合并发生),我仍然不明白这会增加什么复杂性。毕竟,我们可以假设3、4、5都同时发生,然后继续进行6,对吧? - thatDubstepSound

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接