【发布时间】:2010-12-16 02:11:59
【问题描述】:
我想要一个简单的解决方案,在交互式 rebase 期间将两个合并提交压缩在一起。
我的存储库如下所示:
X --- Y --------- M1 -------- M2 (my-feature)
/ / /
/ / /
a --- b --- c --- d --- e --- f (stable)
也就是说,我有一个 my-feature 分支,最近合并了两次,中间没有真正的提交。我不只是想重新设置 my-feature 分支,因为它是它自己的已发布分支,我只想将最后两个合并提交合并为一个(尚未发布这些提交)
X --- Y ---- M (my-feature)
/ /
/ /
a --- ... -- f (stable)
我试过了:
git rebase -p -i M1^
但我得到了:
Refusing to squash a merge: M2
我最后做的是:
git checkout my-feature
git reset --soft HEAD^ # remove the last commit (M2) but keep the changes in the index
git commit -m toto # redo the commit M2, this time it is not a merge commit
git rebase -p -i M1^ # do the rebase and squash the last commit
git diff M2 HEAD # test the commits are the same
现在,新的合并提交不再被视为合并提交(它只保留了第一个父级)。所以:
git reset --soft HEAD^ # get ready to modify the commit
git stash # put away the index
git merge -s ours --no-commit stable # regenerate merge information (the second parent)
git stash apply # get the index back with the real merge in it
git commit -a # commit your merge
git diff M2 HEAD # test that you have the same commit again
但是如果我有很多提交,这可能会变得复杂,你有更好的解决方案吗? 谢谢。
米尔德里德
【问题讨论】:
-
好吧,当你进行第二次合并时,你总是可以使用
--squash来避免创建提交,然后使用git commit --amend修改之前的合并。 -
这不起作用,它不会保存您在提交中合并的分支的新版本
标签: git merge interactive rebase squash