git - 我怎样才能撤消 git reset --hard HEAD~1?

标签 git version-control git-reset

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

git reset --hard HEAD~1

最佳答案

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

$ 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 重置时又放回原位。

关于git - 我怎样才能撤消 git reset --hard HEAD~1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5473/

相关文章:

为单个开发人员设置 git?

version-control - 检索文件的旧版本而不更改工作副本父对象

visual-studio-2010 - 将 TFS 上 checkout 的文件从一台计算机导入到另一台计算机

ruby-on-rails - 如何将 'schema.rb' 重置为最后一个 git head?

git - 如何撤消 git reset --soft 以取回我的更改?

git - 仅还原推送提交的单个文件

bash - 为什么 ". myscript"仅在 Windows sh.exe 的一个 Git 中返回文件未找到?

git - GitHub:致命: 'origin'似乎不是git存储库

git - 如何查看特定 git 提交时的文件?

git - 暂存删除的文件