如何将Git存储库还原到先前的提交? [英] How do I revert a Git repository to a previous commit?

查看:96
本文介绍了如何将Git存储库还原到先前的提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从当前状态恢复为特定提交时创建的快照?

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

如果我执行git log,则得到以下输出:

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.

我该如何从11月3日恢复到提交,即提交0d1d7fc?

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

推荐答案

这在很大程度上取决于您所说的还原".

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

如果您想暂时回到它,四处闲逛,然后回到自己的位置,您所要做的就是检查所需的提交:

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.)

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

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.

另一方面,如果您已经发布了作品,则可能不想重置分支,因为这实际上是在重写历史记录.在这种情况下,您确实可以还原提交.使用Git,还原具有非常特殊的含义:使用反向补丁创建提交以将其取消.这样,您就不会重写任何历史记录.

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 (non inclusive of first hash):
git revert 0d1d7fc..a867b4a

# 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

git-revert联机帮助页实际上在其说明中涵盖了很多内容.另一个有用的链接是此git-scm.com部分讨论git -还原.

如果您确定根本不希望还原,则可以还原该还原(如此处所述),也可以在还原之前将其还原回(请参见上一节).

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).

在这种情况下,您可能还会发现此答案很有帮助:
如何将HEAD移回先前的位置? (独立的头)&撤消提交

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

这篇关于如何将Git存储库还原到先前的提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆