在 github 上撤消 git push

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3692938/
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 09:05:25  来源:igfitidea点击:

Undo a git push on github

gitgithub

提问by Arkan

I made a mistake .... and I don't know how to fix it.

我犯了一个错误......我不知道如何解决它。

I explain the issue.

我解释这个问题。

I was working on my project, and I did a first commit. In this commit 2 big useless files had been added... I didn't wanted these files so I did a

我正在做我的项目,我做了第一次提交。在这次提交中,添加了 2 个无用的大文件......我不想要这些文件,所以我做了一个

git rm file

Then commited again. And I'm stupid, because I pushed to github hehehe :).

然后又犯了。而且我很愚蠢,因为我推到了github hehehe :)。

I think you've found out the problem...

我想你已经发现了问题...

How can I remove definitively these files from my local and github repositories (especially github...)

如何从我的本地和 github 存储库(尤其是 github ...)中明确删除这些文件

I found some help on the internet, but I don't want to break all my repository.

我在互联网上找到了一些帮助,但我不想破坏我所有的存储库。

Thanks

谢谢

回答by Cascabel

If no one else has pulled, you should just get your local branch back to how you want it (probably by either resetting to a previous position, or by doing an interactive rebase to remove the unwanted commit), then push again to github with the -f(force) option:

如果没有其他人拉取,你应该让你的本地分支恢复到你想要的状态(可能通过重置到以前的位置,或者通过执行交互式 rebase 来删除不需要的提交),然后再次推送到 github -f(强制)选项:

git push -f <remote-name> <branch-name>

If other people have pulled, the usual advice applies: read the recovering from upstream rebasesection of the git-rebase man page to see what you're doing to the others before you do your forced update.

如果其他人已经拉了,通常的建议适用:阅读git-rebase 手册页的从上游 rebase 恢复部分,看看你在强制更新之前对其他人做了什么。

回答by Piotr Karbowski

If you wanted remove (not revert, remove) last commit with new files, I think you should do:

如果您想使用新文件删除(不是还原,删除)上次提交,我认为您应该这样做:

git reset --soft "HEAD^"

Anyway since you already pushed it to github, you can't remove it without re-creating git repo. This is how it work, you can revert each commit, for example commit where you deleted those 2 big files. Since it's new repo and you are talking about initial commit, re-creating repo looks for me as best idea.

无论如何,既然你已经把它推送到 github,你不能在不重新创建 git repo 的情况下删除它。这就是它的工作原理,您可以还原每个提交,例如在删除这两个大文件的地方提交。由于它是新的 repo 并且您正在谈论初始提交,因此重新创建 repo 对我来说是最好的主意。