且构网

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

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

更新时间:2021-09-23 23:15:20

这在很大程度上取决于还原"的含义.

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,revert 具有非常特殊的含义:使用反向补丁创建提交以将其取消.这样您就不会重写任何历史记录.

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