如何撤消 git reset --hard HEAD~1? [英] How can I undo git reset --hard HEAD~1?

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

问题描述

是否可以撤消由以下命令引起的更改?如果是,怎么办?

Is it possible to undo the changes caused by the following command? If so, how?

git reset --hard HEAD~1

推荐答案

Pat Notz 是正确的.只要提交是在几天之内,您就可以取回提交.除非您明确告诉它删除较新的 blob,否则 git 只会在大约一个月后进行垃圾收集.

Pat Notz is correct. You can get the commit back so long as it's been within a few days. git only garbage collects after about a month or so unless you explicitly tell it to remove newer blobs.

$ git init
Initialized empty Git repository in .git/

$ echo "testing reset" > file1
$ git add file1
$ git commit -m 'added file1'
Created initial commit 1a75c1d: added file1
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file1

$ echo "added new file" > file2
$ git add file2
$ git commit -m 'added file2'
Created commit f6e5064: added file2
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file2

$ git reset --hard HEAD^
HEAD is now at 1a75c1d... added file1

$ cat file2
cat: file2: No such file or directory

$ git reflog
1a75c1d... HEAD@{0}: reset --hard HEAD^: updating HEAD
f6e5064... HEAD@{1}: commit: added file2

$ git reset --hard f6e5064
HEAD is now at f6e5064... added file2

$ cat file2
added new file

您可以在示例中看到 file2 由于硬重置而被删除,但当我通过 reflog 重置时又将其放回原处.

You can see in the example that the file2 was removed as a result of the hard reset, but was put back in place when I reset via the reflog.

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

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