如何撤消删除我所有文件的“git rm -r . -f”命令?

How to undo " git rm -r . -f " command which deleted my all files?

不知不觉中我删除了所有重要文件。首先,我使用 git init 初始化我的目录,然后使用 git add 添加文件。在那之后取消暂存我的文件我 运行 这个命令 "git rm -r . -f" 从我的本地计算机中删除了我的所有文件。

PS C:\Users\ABDUL WAHAB\Desktop\jup> git init
PS C:\Users\ABDUL WAHAB\Desktop\jup> git add .
PS C:\Users\ABDUL WAHAB\Desktop\jup> git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   .ipynb_checkpoints/Basic_Data_Analysis_Assignment-checkpoint.ipynb
        new file:   .ipynb_checkpoints/Basic_Data_Analysis_Assignment_2-checkpoint.ipynb
        new file:   .ipynb_checkpoints/Basic_Data_Analysis_Assignment_2_Solution-checkpoint.ipynb
        new file:   .ipynb_checkpoints/Basic_Data_Analysis_Assignment_Solution-checkpoint.ipynb
        new file:   .ipynb_checkpoints/Corona Virus Data-checkpoint.ipynb
        new file:   .ipynb_checkpoints/Data_Engineering_Summer_Analytics-checkpoint.ipynb
        new file:   .ipynb_checkpoints/Numpy Tut-checkpoint.ipynb
        new file:   .ipynb_checkpoints/PandaNotes-checkpoint.ipynb
        new file:   .ipynb_checkpoints/Pandas Tut-checkpoint.ipynb
        new file:   .ipynb_checkpoints/Web Scraping- Trump's Lies-checkpoint.ipynb
        new file:   Basic_Data_Analysis_Assignment.ipynb
        new file:   Basic_Data_Analysis_Assignment_2.ipynb
        new file:   Basic_Data_Analysis_Assignment_2_Solution.ipynb
        new file:   Basic_Data_Analysis_Assignment_Solution.ipynb
        new file:   Corona Virus Data.ipynb
        new file:   Data_Engineering_Summer_Analytics.ipynb
        new file:   Numpy Tut.ipynb
        new file:   PandaNotes.ipynb
        new file:   Pandas Tut.ipynb
        new file:   Web Scraping- Trump's Lies.ipynb
        new file:   trump_lies.csv
        new file:   weather.csv
        new file:   weather.txt

和大家一样做了,然后我就做了这个灾难

PS C:\Users\ABDUL WAHAB\Desktop\jup> git rm -r . -f
rm '.ipynb_checkpoints/Basic_Data_Analysis_Assignment-checkpoint.ipynb'
rm '.ipynb_checkpoints/Basic_Data_Analysis_Assignment_2-checkpoint.ipynb'
rm '.ipynb_checkpoints/Basic_Data_Analysis_Assignment_2_Solution-checkpoint.ipynb'
rm '.ipynb_checkpoints/Basic_Data_Analysis_Assignment_Solution-checkpoint.ipynb'
rm '.ipynb_checkpoints/Corona Virus Data-checkpoint.ipynb'
rm '.ipynb_checkpoints/Data_Engineering_Summer_Analytics-checkpoint.ipynb'
rm '.ipynb_checkpoints/Numpy Tut-checkpoint.ipynb'
rm '.ipynb_checkpoints/PandaNotes-checkpoint.ipynb'
rm '.ipynb_checkpoints/Pandas Tut-checkpoint.ipynb'
rm '.ipynb_checkpoints/Web Scraping- Trump's Lies-checkpoint.ipynb'
rm 'Basic_Data_Analysis_Assignment.ipynb'
rm 'Basic_Data_Analysis_Assignment_2.ipynb'
rm 'Basic_Data_Analysis_Assignment_2_Solution.ipynb'
rm 'Basic_Data_Analysis_Assignment_Solution.ipynb'
rm 'Corona Virus Data.ipynb'
rm 'Data_Engineering_Summer_Analytics.ipynb'
rm 'PandaNotes.ipynb'
rm 'Pandas Tut.ipynb'
rm 'Web Scraping- Trump's Lies.ipynb'
rm 'trump_lies.csv'
rm 'weather.csv'
rm 'weather.txt'

谁能帮我恢复那些文件?

您使用 git add 添加的所有内容仍将保留在您的存储库中,至少暂时如此。您可以 运行 git fsck --lost-found 找到悬挂的对象(即不直接或间接与任何引用相关联)。

你会得到这样的输出:

dangling blob 83455250c43390ce653e10935b7b5ae743000ff7
dangling commit b35bb2d9ebf399dc6292a99d00cf75a362405815
dangling commit c082520240cc90f93b0c4c4b045e2011652b8b31
dangling blob a08f72e1406444d4e7196562a5894c5a4838c21c

然后您可以使用 git cat-file blob OBJECT_ID 抓取其中一个 blob 并查看其内容。如果它是您想要保存的内容,只需将其重定向到一个适当命名的文件(例如,git cat-file blob 83455250c43390ce653e10935b7b5ae743000ff7 >weather.txt)。请注意,在这种情况下,您可能对非 blob 不是很感兴趣,因此如果您发现其中任何一个,您可以跳过它们。

此外,我可以建议将来 git rmgit clean 使用 -n (--dry-run) 选项吗?至少我发现它很有用。