且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将Git存储库恢复到以前的提交?

更新时间:2022-10-19 13:47:32

这很大程度上取决于revert的含义。
$ b

暂时切换到另一个提交



如果您想暂时回到它,那么再玩一遍,然后回到您所在的位置,所有你需要做的就是签出所需的提交:

 #这会分离你的HEAD ,也就是说,没有检出分支:
git checkout 0d1d7fc32

或者if你想在你到达的地方进行提交,并继续创建一个新的分支:

  git checkout -b old-state 0d1d7fc32 

要回到原来的位置,只需查看您的分支再次发生。 (如果你已经做了改变,就像往常一样,在切换分支时,你必须适当地处理它们,你可以重新设置它们;你可以隐藏,结帐,隐藏弹出来把它们带走;你可以提交)

硬删除未公布的提交



如果另一方面,你想真正摆脱自那时以来所做的一切,有两种可能性。一,如果你还没有发布任何这些提交,只需重置:

 #这将会销毁任何本地修改。 
#如果您想保留未提交的工作,请不要这样做。
git reset --hard 0d1d7fc32

#或者,如果有工作要保留:
git stash
git reset --hard 0d1d7fc32
git stash pop
#这将保存修改,然后在重置后重新应用该修补程序。
#如果您修改了重置为提交后
#已更改的内容,则可能会发生合并冲突。

如果你搞砸了,你已经抛弃了你的本地修改,但你至少可以得到通过重新设置返回到原来的位置。



撤销已发布的提交提交



另一方面,如果你已经发布了这个工作,你可能不想重置这个分支,因为这实际上是在重写历史。在那种情况下,你确实可以恢复提交。使用Git,回复具有非常明确的含义:使用反向修补程序创建提交以将其取消。这样你就不会重写任何历史记录。

#这将创建三个独立的还原提交:
git还原a867b4af 25eee4ca 0766c053

#它也需要范围。这将恢复最后两次提交:
git revert HEAD〜2..HEAD

#类似地,你可以使用提交哈希恢复一系列提交:
git还原a867b4af。 .0766c053

#恢复合并提交
git revert -m 1< merge_commit_sha>

#为了得到一个,你可以使用`rebase -i`来压缩它们
#或者,你可以手动完成它(一定要在回购的顶层执行此操作)
#让你的索引和工作树进入想要的状态,而不改变HEAD:
git checkout 0d1d7fc32。

#然后提交。一定要写出一条好消息来描述你刚刚做了什么
git commit

git-revert 手册页实际上涵盖了其描述中有很多这样的内容。另一个有用的链接是这个git-scm.com部分讨论git如果您决定完全不想还原,您可以恢复回复(如此处所述)或重置回到之前的状态。

(请参阅上一节)。

您也可以在此情况下找到此答案:

如何将HEAD移回到以前的位置? (分离头)


How do I revert from my current state to a snapshot made on a certain commit?

If I do git log, then I get the following output:

$ git log
commit a867b4af366350be2e7c21b8de9cc6504678a61b`
Author: Me <me@me.com>
Date:   Thu Nov 4 18:59:41 2010 -0400

blah blah blah...

commit 25eee4caef46ae64aa08e8ab3f988bc917ee1ce4
Author: Me <me@me.com>
Date:   Thu Nov 4 05:13:39 2010 -0400

more blah blah blah...

commit 0766c053c0ea2035e90f504928f8df3c9363b8bd
Author: Me <me@me.com>
Date:   Thu Nov 4 00:55:06 2010 -0400

And yet more blah blah...

commit 0d1d7fc32e5a947fbd92ee598033d85bfc445a50
Author: Me <me@me.com>
Date:   Wed Nov 3 23:56:08 2010 -0400

Yep, more blah blah.

How do revert to the commit from November 3, i.e. commit 0d1d7fc?

This depends a lot on what you mean by "revert".

Temporarily switch to a different commit

If you want to temporarily go back to it, fool around, then come back to where you are, all you have to do is check out the desired commit:

# This will detach your HEAD, that is, leave you with no branch checked out:
git checkout 0d1d7fc32

Or if you want to make commits while you're there, go ahead and make a new branch while you're at it:

git checkout -b old-state 0d1d7fc32

To go back to where you were, just check out the branch you were on again. (If you've made changes, as always when switching branches, you'll have to deal with them as appropriate. You could reset to throw them away; you could stash, checkout, stash pop to take them with you; you could commit them to a branch there if you want a branch there.)

Hard delete unpublished commits

If, on the other hand, you want to really get rid of everything you've done since then, there are two possibilities. One, if you haven't published any of these commits, simply reset:

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to.

If you mess up, you've already thrown away your local changes, but you can at least get back to where you were before by resetting again.

Undo published commits with new commits

On the other hand, if you've published the work, you probably don't want to reset the branch, since that's effectively rewriting history. In that case, you could indeed revert the commits. With Git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out. This way you don't rewrite any history.

# This will create three separate revert commits:
git revert a867b4af 25eee4ca 0766c053

# It also takes ranges. This will revert the last two commits:
git revert HEAD~2..HEAD

#Similarly, you can revert a range of commits using commit hashes:
git revert a867b4af..0766c053 

# Reverting a merge commit
git revert -m 1 <merge_commit_sha>

# To get just one, you could use `rebase -i` to squash them afterwards
# Or, you could do it manually (be sure to do this at top level of the repo)
# get your index and work tree into the desired state, without changing HEAD:
git checkout 0d1d7fc32 .

# Then commit. Be sure and write a good message describing what you just did
git commit

The git-revert manpage actually covers a lot of this in its description. Another useful link is this git-scm.com section discussing git-revert.

If you decide you didn't want to revert after all, you can revert the revert (as described here) or reset back to before the revert (see the previous section).

You may also find this answer helpful in this case:
How to move HEAD back to a previous location? (Detached head)