我可以取回因 运行 git restore -s@ -SW 而消失的工作树中的文件吗?

Can I get back the files in working tree which disappeared resulting from running git restore -s@ -SW?

我忘记将 .ai 添加到我的 .gitignore 文件中,但是 运行 $ git add . 并在索引中添加了相当多的 .ai 个文件。

当我运行 $ git status 看到它们,我想将它们从索引中删除。

匆忙中,我运行 $ git restore -s@ -SW 导致我所有的 .ai 文件都从我的工作树中消失了。我不应该使用 W.

有什么方法可以取回我的 .ai 文件?

首先,检查您是否仍然可以通过“本地历史记录”访问这些文件(由您的 IDE 记录,而不是 Git)

例如,Local History VSCode extension, as ,可以提供帮助。

如果没有,请仔细检查您的 OS 备份功能,例如 MacOS Time Machine status,以防您可以从备份中恢复它们。

最后,由于您已将这些文件添加到索引中,check git fsck

git fsck --cache --no-reflogs --lost-found --unreachable HEAD

根据列出的 SHA1,您可以执行 git show:

git show "<SHA-1 REFERENCE TO THE BLOB OBJECT HERE>" > lost_file.txt

torek in 所述:

Note that you can also find the contents of each of those "unreachable blob"s in the .git/lost-found/other directory.

This is often quicker and easier than git show since you can cd .git/lost-found/other, and then grep expected-string * to find the file(s) that have the expected string.