使用git rm -rf恢复目录

时间:2016-03-22 18:44:45

标签: git

我使用git add将目录添加到本地git仓库,然后意外地执行git rm -rf(之前没有提交)。有没有办法从这个目录中恢复文件?

1 个答案:

答案 0 :(得分:7)

是的,你可以,

请阅读:How to undo changes in Git

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

他们刚刚被添加而且从未被提交,他们被称为dangling objects。那些对象可以恢复。

何恢复悬空物体?

首先我们必须找到它们

git fsck --full

enter image description here

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

# 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.

enter image description here