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

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

问题描述

最简单的撤销方法是什么:

What's the simplest way to undo:

git reset HEAD~

目前,我能想到的唯一方法是从远程仓库执行 git clone http://....

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

推荐答案

简答:

git reset 'HEAD@{1}'

长答案:

Git 保存所有 ref 更新的日志(例如,签出、重置、提交、合并).您可以输入以下内容查看:

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

第一行说HEAD0个位置前(即当前位置)是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,那么您想要的提交将不会位于列表的顶部,您将需要搜索 引用日志.

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天全站免登陆