如何撤消“git reset”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2510276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to undo 'git reset'?
提问by Andriy Drozdyuk
What's the simplest way to undo the
最简单的撤销方法是什么
git reset HEAD~
command? Currently, the only way I can think of is doing a "git clone http://..." from a remote repo.
命令?目前,我能想到的唯一方法是从远程仓库执行“git clone http://...”。
回答by Mark Lodato
Short answer:
简短的回答:
git reset 'HEAD@{1}'
Long answer:
长答案:
Git keeps a log of all ref updates (e.g., checkout, reset, commit, merge). You can view it by typing:
Git 保留所有 ref 更新的日志(例如,检出、重置、提交、合并)。您可以通过键入以下内容查看它:
git reflog
Somewhere in this list is the commit that you lost. Let's say you just typed git reset HEAD~
and want to undo it. My reflog looks like this:
此列表中的某处是您丢失的提交。假设您刚刚输入git reset HEAD~
并想要撤消它。我的 reflog 看起来像这样:
$ git reflog
3f6db14 HEAD@{0}: HEAD~: updating HEAD
d27924e HEAD@{1}: checkout: moving from d27924e0fe16776f0d0f1ee2933a0334a4787b4c
[...]
The first line says that HEAD
0 positions ago (in other words, the current position) is 3f6db14; it was obtained by resetting to HEAD~
. The second line says that HEAD
1 position ago (in other words, the state before the reset) is d27924e. It was obtained by checking out a particular commit (though that's not important right now). So, to undo the reset, run git reset HEAD@{1}
(or git reset d27924e
).
第一行说HEAD
0个位置之前(换句话说,当前位置)是3f6db14;它是通过重置为 获得的HEAD~
。第二行表示HEAD
1 个位置之前(换句话说,重置前的状态)是 d27924e。它是通过检查特定提交获得的(尽管现在这并不重要)。因此,要撤消重置,请运行git reset HEAD@{1}
(或git reset d27924e
)。
If, on the other hand, you've run some other commands since then that update HEAD, the commit you want won't be at the top of the list, and you'll need to search through the reflog
.
另一方面,如果您从那时起运行了一些其他命令来更新 HEAD,那么您想要的提交将不会位于列表的顶部,您将需要搜索reflog
.
One final note: It may be easier to look at the reflog
for the specific branch you want to un-reset, say master, rather than HEAD
:
最后一个注意事项:查看reflog
要取消重置的特定分支可能更容易,例如 master,而不是HEAD
:
$ git reflog show master
c24138b master@{0}: merge origin/master: Fast-forward
90a2bf9 master@{1}: merge origin/master: Fast-forward
[...]
This should have less noise it in than the general HEAD reflog
.
这应该比一般的噪音小吧HEAD reflog
。
回答by Dan Fischer
Old question, and the posted answers work great. I'll chime in with another option though.
老问题,发布的答案效果很好。不过,我会加入另一种选择。
git reset ORIG_HEAD
git reset ORIG_HEAD
ORIG_HEAD
references the commit that HEAD
previously referenced.
ORIG_HEAD
引用HEAD
先前引用的提交。
回答by zainengineer
My situation was slightly different, I did git reset HEAD~
three times.
我的情况略有不同,我做了git reset HEAD~
3次。
To undo it I had to do
要撤消它,我必须这样做
git reset HEAD@{3}
so you should be able to do
所以你应该能够做到
git reset HEAD@{N}
But if you have done git reset using
但是如果你已经完成了 git reset 使用
git reset HEAD~3
you will need to do
你需要做
git reset HEAD@{1}
As {N} represents number of operations in Reflog. As Mark pointed out in the comments.
由于 {N} 表示 Reflog 中的操作数。正如马克在评论中指出的那样。
回答by omilus
1.Use git reflog
to get all references update.
1.git reflog
用于获取所有参考更新。
2.git reset <id_of_commit_to_which_you_want_restore>
2.git reset <id_of_commit_to_which_you_want_restore>