Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

git - 如何将Git存储库还原到先前的提交?(How do I revert a Git repository to a previous commit?)

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 ,则得到以下输出:)

$ git log
commit a867b4af366350be2e7c21b8de9cc6504678a61b`
Author: Me <[email protected]>
Date:   Thu Nov 4 18:59:41 2010 -0400

blah blah blah...

commit 25eee4caef46ae64aa08e8ab3f988bc917ee1ce4
Author: Me <[email protected]>
Date:   Thu Nov 4 05:13:39 2010 -0400

more blah blah blah...

commit 0766c053c0ea2035e90f504928f8df3c9363b8bd
Author: Me <[email protected]>
Date:   Thu Nov 4 00:55:06 2010 -0400

And yet more blah blah...

commit 0d1d7fc32e5a947fbd92ee598033d85bfc445a50
Author: Me <[email protected]>
Date:   Wed Nov 3 23:56:08 2010 -0400

Yep, more blah blah.

How do revert to the commit from November 3, ie commit 0d1d7fc ?

(从11月3日起如何恢复到提交,即commit 0d1d7fc ?)

  ask by Crazy Serb translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

(使用Git,还原具有非常特殊的含义:使用反向补丁创建提交以将其取消。)

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.

(git-revert实际上在其描述中涵盖了很多内容。)

Another useful link is this git-scm.com section discussing git-revert .

(另一个有用的链接是git-scm.com讨论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?

(如何将HEAD移回先前的位置?)

(Detached head)

((独立头))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...