简体   繁体   English

Git Squash并删除以前的提交

[英]Git Squash and remove previous commits

Maybe I misunderstood how GIT works. 也许我误解了GIT的工作原理。

I've run git rebase -i HEAD~10 and I could squash 10 commits into one. 我已经运行git rebase -i HEAD~10 ,我可以将10次提交压缩为一个。 The issue is that all squashed commits are still there, and I thought they were going to be dropped after merging them all into one . 问题是所有压缩的提交仍然存在,并且我认为在将它们全部合并为一个之后, 它们将被丢弃。

Is this the expected result? 这是预期的结果吗? If so, can I rewrite the history to remove useless commits (since these changes are already in the commit which all previous commits were squashed)? 如果是这样,我是否可以重写历史记录以删除无用的提交(因为这些更改已经存在于以前所有提交已被压缩的提交中)?

When you began your interactive rebase session, you should have been prompted with a list of the last 10 commits from the current branch: 在开始交互式rebase会话时,应该提示您当前分支中最近10次提交的列表:

git rebase -i HEAD~10

pick as2i8dw first commit
pick ee361eb second  commit
...
pick b2762sx most recent commit

You need to change this file to the following: 您需要将此文件更改为以下内容:

pick as2i8dw first commit
squash ee361eb second commit
...
squash b2762sx most recent commit

Then you need to do a git commit to save the changes. 然后,您需要执行git commit来保存更改。 Now when doing a git log you should only see the as2i8dw commit and none of the other ten. 现在,当执行git日志时,您应该只会看到as2i8dw提交,而其他十个都看不到。

That being said, is this what you did? 话虽这么说,这是你做的吗?

The issue is that all squashed commits are still there 问题是所有压缩的提交仍然存在

If those commits are still accessible by any other reference (other branch or tag), there would still be visible, even after the current branch is rebased. 如果仍然可以通过任何其他引用(其他分支或标记)访问那些提交,则即使在重新定义当前分支之后,该提交仍然可见。

Try instead squashing the commits with a git reset --soft . 尝试使用git reset --soft
If HEAD does still reference your 10 commits: 如果HEAD仍然引用您的10次提交:

git reset --soft HEAD~10
git commit -m "squashed 10 commits"

I faced the similar issue and figured out the actual cause for it: The flow: 我遇到了类似的问题,并找出了导致该问题的实际原因:流程:

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

Then now when you git pull from your remote branch, it will pull the rest of the commits which is not there in local ( basically all the commits you had squashed previously, since it is now present in one commit) and hence you are seeing the previous commits as well. 然后,现在当您从远程分支进行git pull时,它将拉出本地中不存在的其余提交(基本上是您之前压缩过的所有提交,因为它现在在一个提交中),因此您看到了以前的提交。

Better way is to git push -f to your remote branch, if you are confident that you have no new changes added there. 如果您确信没有在其中添加新的更改,更好的方法是git push -f到您的远程分支。

PS: If you have any new changes in the remote branch, better to: PS:如果您在远程分支中有任何新更改,最好执行以下操作:

git rebase origin remote_branch 

and then squash your commits. 然后压缩您的提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM