如何撤消 git push?

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

How to undo a git push?

git

提问by Jwan622

I pushed up some files up to github using git push <branch_name>. When I open a PR, the files show up in the changes. How do I undo this git push? In short, I tried to do a git resetof that file before pushing but apparently I mistyped the filename. Now what do I do?

我使用git push <branch_name>. 当我打开 PR 时,文件显示在更改中。我如何撤消这个git push?简而言之,我尝试git reset在推送之前执行该文件的操作,但显然我输错了文件名。现在我该怎么办?

How do I undo pushes to Github?

如何撤消对 Github 的推送?

回答by Will

First, make a backup, in case something goes wrong. Clone your repository as <repository>-backupwith -b <branchname>and don't touch that one.

首先,进行备份,以防出现问题。克隆版本库中按照<repository>-backup-b <branchname>和不碰那一个。

Second, find the commit hash of the last good commit in git log.

其次,在git log.

Then run:

然后运行:

git push --force origin <last good commit hash>:<branch>

Note that rewriting history is generally considered a bad idea, unless you're the only user of the repository. If other people have pulled down the repository with a commit change, and you force push and remove that commit, the repository will enter a corrupted state.

请注意,重写历史通常被认为是一个坏主意,除非您是存储库的唯一用户。如果其他人通过提交更改拉取了存储库,并且您强制推送并删除该提交,则存储库将进入损坏状态。

Unless you pushed some sensitive information you really need to remove, just revert the commit with:

除非您推送了一些确实需要删除的敏感信息,否则只需使用以下命令还原提交:

git revert <commit hash to revert>
git push