简体   繁体   English

GIT…正在还原

[英]GIT… reverting

I have to revert to an earlier commit after my stuff has been already pushed and deployed. 在我的内容已经推送和部署之后,我必须恢复到较早的提交。

I have used git checkout HEAD^ 我已经使用git checkout HEAD ^

to check it out, but how to push it now? 检查一下,但是现在如何推? it says branch is uptodate! 它说分支是最新的!

Any help is greatly appreciated!""" 任何帮助是极大的赞赏!”””

You have two options. 您有两个选择。 You can revert the erroneous commit with git revert HEAD . 您可以使用git revert HEAD还原错误的提交。 This will generate a new commit which undoes the previous commit. 这将生成一个新的提交,它将撤消之前的提交。 You can then push that as with any other commit. 然后,您可以像其他提交一样将其推送。

If you want the history to show the commit never existed, you can remove the commit with git reset --hard HEAD^ . 如果您希望历史记录显示提交不存在,则可以使用git reset --hard HEAD^删除提交。 (Note that this will discard any changes you made to the working directory. You can then push with the -f flag to force the commit to be removed from the remote repository. Note when doing this that others may run into problems if they have fetched the commit you remove. More importantly, if anyone has pushed anything since your commit, those commits will be lost. If you're not sure if anyone else has accessed the repo, the first options is safest. (请注意,这将丢弃您对工作目录所做的任何更改。然后,您可以使用-f标志进行推送,以强制将提交从远程存储库中删除。注意,这样做时,如果其他人将其取回,可能会遇到问题。更重要的是,如果在提交后有人推送了任何内容,则这些提交将丢失。如果您不确定是否有人访问过该回购协议,则最安全的选择是第一个。

EDIT 编辑

As Jefromi mentions, you will want to check out the branch you were working on before doing either of the above or you will cause additional problems. 正如Jefromi所提到的,您将要在执行上述任一操作之前先检查您正在处理的分支,否则会导致其他问题。

git checkout

just checkout the old version. 只需签出旧版本即可。 It doesn't really change anything. 它并没有真正改变任何东西。 So to revert a commit you need to commit an opposite changes. 因此,要还原提交,您需要提交相反的更改。 Hopefully, there is a speciall command for this 希望对此有一个特殊的命令

git revert *commit-id*

So to revert the last commit type 所以要还原最后的提交类型

git revert HEAD

But you can't revert merge commit that way. 但是您不能以这种方式还原合并提交。 To do this you need to specify the parent to which to revert. 为此,您需要指定要还原到的父级。 The list of parent can be viewed by this command 可以通过此命令查看父级列表

git cat-file -p HEAD

Determine the number of the desired commit (the first or the second) and then run 确定所需提交的数量(第一个或第二个),然后运行

git revert -m 1 HEAD

or 要么

git revert -m 2 HEAD

You have to commit the changes first. 您必须先提交更改。

Also, if you only want to revert a single commit, you can use git revert <commit> instead of git checkout . 另外,如果只想还原单个提交,则可以使用git revert <commit>代替git checkout


If you did just git checkout HEAD^ , there'll be nothing to commit anyway; 如果您只是执行git checkout HEAD^ ,无论如何将没有任何内容可提交; you will be on a "detached branch". 您将处于“独立分支”。 If you did something like git checkout HEAD^ -- . 如果您执行了git checkout HEAD^ -- . then you will have to commit the staged changes before pushing. 那么您将必须先提交分阶段的更改,然后再进行推送。

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

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