![](/img/trans.png)
[英]git automatically squash commits to previous commits that changed the same file/s
[英]Git Squash and remove previous commits
也许我误解了GIT的工作原理。
我已经运行git rebase -i HEAD~10
,我可以将10次提交压缩为一个。 问题是所有压缩的提交仍然存在,并且我认为在将它们全部合并为一个之后, 它们将被丢弃。
这是预期的结果吗? 如果是这样,我是否可以重写历史记录以删除无用的提交(因为这些更改已经存在于以前所有提交已被压缩的提交中)?
在开始交互式rebase会话时,应该提示您当前分支中最近10次提交的列表:
git rebase -i HEAD~10
pick as2i8dw first commit
pick ee361eb second commit
...
pick b2762sx most recent commit
您需要将此文件更改为以下内容:
pick as2i8dw first commit
squash ee361eb second commit
...
squash b2762sx most recent commit
然后,您需要执行git commit
来保存更改。 现在,当执行git日志时,您应该只会看到as2i8dw
提交,而其他十个都看不到。
话虽这么说,这是你做的吗?
问题是所有压缩的提交仍然存在
如果仍然可以通过任何其他引用(其他分支或标记)访问那些提交,则即使在重新定义当前分支之后,该提交仍然可见。
尝试使用git reset --soft
。
如果HEAD仍然引用您的10次提交:
git reset --soft HEAD~10
git commit -m "squashed 10 commits"
我遇到了类似的问题,并找出了导致该问题的实际原因:流程:
git rebase -i HEAD~10
# Retain the first commit from below( as pick) and change the rest of the `pick` to `squash`
# After your rebase is successful
git log
# You can see all your commits squashes to one commit
然后,现在当您从远程分支进行git pull
时,它将拉出本地中不存在的其余提交(基本上是您之前压缩过的所有提交,因为它现在在一个提交中),因此您看到了以前的提交。
如果您确信没有在其中添加新的更改,更好的方法是git push -f
到您的远程分支。
PS:如果您在远程分支中有任何新更改,最好执行以下操作:
git rebase origin remote_branch
然后压缩您的提交。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.