如何撤消'git reset'? [英] How to undo 'git reset'?

查看:120
本文介绍了如何撤消'git reset'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

撤消

git reset HEAD~

命令? 目前,我唯一想到的方法是从远程存储库中进行"git clone http://...".

command? Currently, the only way I can think of is doing a "git clone http://..." from a remote repo.

推荐答案

简短答案:

git reset 'HEAD@{1}'

长答案:

Git保留所有参考更新的日志(例如,签出,重置,提交,合并).您可以通过键入以下内容进行查看:

Long answer:

Git keeps a log of all ref updates (e.g., checkout, reset, commit, merge). You can view it by typing:

git reflog

此列表中的某处是您丢失的提交.假设您刚刚输入git reset HEAD~并想要撤消它.我的引用日志看起来像这样:

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 reflog
3f6db14 HEAD@{0}: HEAD~: updating HEAD
d27924e HEAD@{1}: checkout: moving from d27924e0fe16776f0d0f1ee2933a0334a4787b4c
[...]

第一行说HEAD 0个位置之前(换句话说,当前位置)是3f6db14;它是通过重置为HEAD~获得的.第二行说HEAD 1位置之前(换句话说,复位之前的状态)是d27924e.它是通过签出特定提交(尽管现在不重要)而获得的.因此,要撤消重置,请运行git reset HEAD@{1}(或git reset d27924e).

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,则所需的提交将不在列表的顶部,并且您需要在reflog中进行搜索

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.

最后一点:在reflog上查找要取消重置的特定分支(例如master)可能比HEAD更容易:

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:

$ git reflog show master
c24138b master@{0}: merge origin/master: Fast-forward
90a2bf9 master@{1}: merge origin/master: Fast-forward
[...]

与普通的HEAD reflog相比,它的噪音应该更少.

This should have less noise it in than the general HEAD reflog.

这篇关于如何撤消'git reset'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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