简体   繁体   English

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

[英]How can I revert a push to repository in GitHub

I commited few changes, and made a push to remote branch in github. 我提交了一些更改,并推动了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? 有没有办法恢复推动?

The git revert command does not rewrite history, but does take away the changes from a commit with a new commit. git revert命令不会重写历史记录,但会通过新提交从提交中删除更改。 Then all you would have to do is to push again. 那么你所要做的就是再次推动。 This is the suggested method. 这是建议的方法。

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). 如果你真的想让github看起来像一个特定的提交从未真正发生过,有一种方法,但要谨慎(特别是如果其他用户贡献给github存储库)。 In fact if others use this github repository, stop now and use the method mentioned in the first paragraph. 事实上,如果其他人使用这个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 rebase to get your local directory to match what you want github to look like before doing the git push -f . 如果它不是最后一次推送,请参阅git cherry-pickgit rebase手册页,以使本地目录与gitub在执行git push -f之前的样子相匹配。 If you do not supply the -f option to git push , you will not rewrite history. 如果你没有为git push提供-f选项,你将不会重写历史记录。 And any push will fail if it attempts to rewrite history. 如果尝试重写历史记录,任何推送都将失败。 The -f option should not be used by default. -f选项应该不会被默认使用。

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 (ie 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 revert and 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. 默认情况下,Git将在HEAD之上创建其他提交,以说明要还原的更改。 Check out the manual page for git revert on further details on how to use it. 有关如何使用它的更多详细信息,请查看git revert手册页

After making the reverts you need, simply push up again! 在完成所需的恢复之后,只需再次向上推!

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

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