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

git reset --hard HEAD~1

当前回答

如果你真的很幸运,就像我一样,你可以回到你的文本编辑器,点击“撤销”。

我知道这确实不是一个正确的答案,但它节省了我半天的工作,所以希望它能为其他人做同样的事情!

其他回答

您要做的是指定要还原到的提交的sha1

git reset --hard <sha1 of desired commit>

但不要等太久。。。几周后,git将最终看到提交未被引用,并删除所有blob。

gitreset-hard-您可以使用它还原一个页面,然后可以再次隐藏或从原始页面中提取所有内容

数字刷新

在列表中找到提交sha,然后将其复制并粘贴到以下命令中:

git cherry pick<the sha>

帕特·诺茨是正确的。你可以在几天内收回承诺。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尚未垃圾收集,则可以恢复它。

使用fsck获取悬空提交的概述:

$ git fsck --lost-found
dangling commit b72e67a9bb3f1fc1b64528bcce031af4f0d6fcbf

使用rebase恢复悬空提交:

$ git rebase b72e67a9bb3f1fc1b64528bcce031af4f0d6fcbf