我可以撤消最后一次 git push 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3012929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:33:29  来源:igfitidea点击:

Can I undo the last git push?

git

提问by Stray

A team member accidentally pushed half a gig of unwanted zips to the remote repo last night when they were in a rush. Yes... oops.

昨晚,一名团队成员在匆忙时不小心将半场不需要的拉链推到了远程仓库。是的……哎呀。

Nobody has pulled or committed since.

从那以后没有人拉或犯过。

Ideally I want to just 'undo' what happened.

理想情况下,我只想“撤消”发生的事情。

I have looked at filter-branch and was thinking of trying something like

我看过 filter-branch 并且正在考虑尝试类似的东西

git filter-branch --tree-filter 'rm -f *.zip' HEAD

but that would be local, and I can't figure out how to do it direct on the remote repo.

但这将是本地的,我无法弄清楚如何直接在远程存储库上执行此操作。

Is there a simpler way to undo what happened? If she amends her last commit and pushes again will that undo the push - ie actually remove those files from the history?

有没有更简单的方法来撤销发生的事情?如果她修改上次提交并再次推送,是否会撤消推送 - 即实际上从历史记录中删除这些文件?

Obviously if she deletes them, commits and pushes again then that still leaves the content in the repo, which is no good.

显然,如果她删除它们,再次提交并推送,那么内容仍然留在 repo 中,这是不好的。

回答by Stray

Thanks Don, I'd seen that but somehow hadn't realised it fixed my problem, because I only have one branch.

谢谢唐,我已经看到了,但不知何故没有意识到它解决了我的问题,因为我只有一个分支。

I did:

我做了:

git push -f origin 5910117a8fc2c71334251465b54d6d9daeb28d1c:master

And it's all back to how it was.

而这一切又回到了原来的样子。

回答by michid

I think

我认为

git reset --hard HEAD^
git push -f

should to the trick: It resets your local checkout to the previous commit (assuming the last one is the one you want to drop) and force pushes it to the remote repository.

应该有诀窍:它将您的本地结帐重置为前一个提交(假设最后一个是您要删除的提交)并强制将其推送到远程存储库。