我如何在早些时候git squash一起提交?

时间:2017-02-20 18:42:24

标签: git

我的分支上有大约10个提交,让我们说ID 1到10.我想要将4,5和6的提交压缩在一起,但其余部分保持不变。我找到的每个教程都从当前的HEAD开始,然后使用git rebase -i HEAD~(x amount)向后退。但是如何在之前的提交中启动rebase并指定一个范围,例如git rebase -i 6 ... 4?

谢谢

2 个答案:

答案 0 :(得分:4)

只需做一个互动的rebase,修复你想要压扁或修复的提交。

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

答案 1 :(得分:1)

嗯,@ jaredr已经回答了你的问题。来到问题的第二部分,GIT没有提供这样的命令(git rebase -i 4..6)。事实上它不应该!想象一下,如果GIT开始提供这样的命令(必须在某些提交上执行操作并堆叠其他命令),GIT必须执行多少操作。正确合并会增加额外的复杂性。因此,它将此作业转移给用户本身/她是更了解代码的人。)此外,它提供了Git squash等帮助机制,您可以暂时转储提交并处理其他内容。