使用git rm -rf恢复目录

Grz*_*ała 1 git

我添加了一个目录到本地git repo使用git add然后意外地做了git rm -rf(没有提交之前).有没有办法从这个目录中恢复文件?

Cod*_*ard 7

是的你可以,

阅读:如何撤消Git中的更改.


将文件添加到git后,它们将开始被跟踪,并存储在该.git文件夹下.

在他们刚刚被添加,从未提交他们被裁判为dangling objects.那些对象可以恢复.


何恢复悬空物体?

首先,我们必须找到它们

git fsck --full
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

获得这些对象的列表后,您需要查看它们并找出所需的对象.

# Print out the content of the Object
git cat-file -p <SHA-1>

# If the object is a file you will simply see its content,
# Copy it and save it in a new file.
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述