git 如何将推送恢复到 GitHub 中的存储库

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

How can I revert a push to repository in GitHub

git

提问by user2916464

I commited few changes, and made a push to remote branch in github. But then I realized that I did some mistakes in the commit, and pushed lot of wrong files. Is there a way to revert the push?

我提交了一些更改,并推送到 github 中的远程分支。但是后来我意识到我在提交中犯了一些错误,并且推送了很多错误的文件。有没有办法恢复推送?

回答by cforbish

The git revertcommand does not rewrite history, but does take away the changes from a commit with a new commit. Then all you would have to do is to push again. This is the suggested method.

git revert命令不会重写历史记录,但会从带有新提交的提交中删除更改。然后你所要做的就是再次推动。这是建议的方法。

If you really want to make github look like a particular commit never really happened, there is a way, but use caution (especially if other users contribute to the github repository). In fact if others use this github repository, stop now and use the method mentioned in the first paragraph.

如果你真的想让 github 看起来像一个从未真正发生过的特定提交,有一种方法,但要小心(特别是如果其他用户对 github 存储库做出贡献)。事实上,如果其他人使用这个github存储库,现在停止并使用第一段提到的方法。

If this is your own private github repository and the last push is what you need to take away (assumes you are still on the same local branch):

如果这是您自己的私有 github 存储库并且最后一次推送是您需要带走的(假设您仍在同一个本地分支上):

git reset --hard HEAD~
git push -f

If it is not the last push, see man pages for either git cherry-pick, or git rebaseto get your local directory to match what you want github to look like before doing the git push -f. If you do not supply the -foption to git push, you will not rewrite history. And any push will fail if it attempts to rewrite history. The -foption should notbe used by default.

如果不是最后一推,看到任何人的页面git cherry-pick,或者git rebase让你的本地目录来匹配你想要什么github上看起来像在做之前git push -f。如果您不为 提供-f选项git push,则不会重写历史记录。如果尝试重写历史记录,任何推送都会失败。该-f选项应该不会被默认使用。

回答by miqh

Keep in mind that once you have pushed your commits to a remote repository that you should exercise caution, especially so if you have some intention of rewriting history (i.e. removing all your undesired commits by rebasing).

请记住,一旦您将提交推送到远程存储库,您就应该谨慎行事,尤其是如果您打算重写历史记录(即通过变基删除所有不需要的提交)。

If you do not care about history, then reverting your bad changes can be done by using git revertand passing it the commit identifiers you want to undo.

如果您不关心历史记录,那么可以通过使用git revert并向其传递您想要撤消的提交标识符来恢复您的错误更改。

For example, if you have changes contained in two commits, A and B, you would like to remove:

例如,如果您在两个提交 A 和 B 中包含更改,您想删除:

git revert A B

This will revert commits A and B, creating a commit for each.

这将还原提交 A 和 B,为每个提交创建一个提交。

By default, Git will create additional commits on top of HEAD to illustrate changes being reverted. Check out the manual pagefor git reverton further details on how to use it.

默认情况下,Git 将在 HEAD 之上创建额外的提交来说明正在恢复的更改。查看手册页git revert了解有关如何使用它的更多详细信息。

After making the reverts you need, simply push up again!

完成您需要的还原后,只需再次向上推!