简体   繁体   English

如何从丢失的树和悬垂的Blob中恢复文件?

[英]How to recover files from missing tree and dangling blobs?

After git add . git add .之后git add . I ran git rm -r -f . 我跑了git rm -r -f . ,and all my local files were deleted accidentally. ,而我的所有本地文件均被意外删除。 After recovering them with git fsck --lost-found I have this in lost-found/other directory: git fsck --lost-found恢复它们之后,我在lost-found/other目录中有文件:

git fsck --full notice: HEAD points to an unborn branch (master) Checking object directories: 100% (256/256), done. notice: No default references dangling blob 0453ac74117a3e02ae1169ac28e6df356baec5b6 dangling blob 871364bedccaa2c57054509feeb60955e70bd141 dangling blob c38f2c2aead5f278b7680a0629ee56682264f9d3 dangling blob c2ad8101cfcf3d21e33c8da3f0677b5b9cc58c30 dangling blob c5d98571c464030b61978b6812fa3579c7294b99 missing tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904

After looking at some dangling blobs, I realized that although they have the deleted file's content but also some gibberish along with: 在查看了一些悬空的Blob之后,我意识到,尽管它们具有已删除文件的内容,但还带有一些乱码:

slug_fieldtidtMetacBseZeZdZRS(tcreatedtownertimage(screatedsownersimage(R(R)Rtmodeltfields(((sE/home/invinciblycool/PycharmProjects/jotter_api/jotter/serializers.pyR1HsN(( R(R)R tNonetTrueR4RtSlugRelatedFieldR3R1(((sE/home/invinciblycool/PycharmProjects/jotter_api/jotter/serializers.pyR+?s tNoteSerializercBs3eZejdedd Zddd YZRS(RR/R0R1cBseZeZdZRS(R3R2ttext(sownerscreatedstext(R(R)RR5R6(((sE/home/invinciblycool/PycharmProjects/jotter_api/jotter/serializers.pyR1Ss((R(R)RR9R8R3R1(((sE/home/invinciblycool/PycharmProjects/jotter_api/jotter/serializers.pyR:M (RRtrest_frameworkRtmodelsRRR t ImageFieldR

How do I recover these files, actually from the gibberish I am not even sure if these were the files. 我如何恢复这些文件,实际上我什至从这些乱七八糟的文件中也不知道这些文件。 FYI the Javascript files seem to be clean but not the other python and html files. 仅供参考,Javascript文件似乎很干净,但其他python和html文件却不是。

I tried to reproduce what you did: 我试图重现你的所作所为:

1) git add . 1) git add .

2) rm -r -f * 2) rm -r -f *

after this I think that you made a git reset , or git reset --hard (wrong choice: it overwrote the index with an empty one because you never did a commit)... 之后,我认为您进行了git resetgit reset --hard (错误的选择:因为您从未执行过提交,所以用空的索引覆盖了索引)...

but the correct command were as indicated in my previous answer git checkout-index -af (but this is useless after the reset) 但是正确的命令如我先前的回答git checkout-index -af (但是在重置后没有用)

at this point you gave correctly the git fsck --lost-found , my suggestion was equivalent with lower level commands ( git cat-file... ) 在这一点上,您正确地给出了git fsck --lost-found ,我的建议等同于较低级别的命令( git cat-file...

so: why gibberish? 如此:为什么乱七八糟?

Are you sure that in the directory there was only source code files and not (for example) compiled versions of the python files (extension .pyc)? 您确定目录中只有源代码文件,而没有(例如)python文件的编译版本(扩展名为.pyc)吗? Perhaps in some hidden directory created by a IDE? 也许在IDE创建的某些隐藏目录中?

1) rm -r -f . 1) rm -r -f . should abort without effect probably you did rm -r -f * and it does not delete the .git directory 应该中止而无效,可能您执行了rm -r -f *并且它不会删除.git目录

2) this is confirmed by the fact that the command git fsck --lost-found did not abort 2)这是由以下事实证实的: git fsck --lost-found命令没有中止

in this situation (deletion of just the working directory) the correct command to recover as much as possible is : 在这种情况下(仅删除工作目录) ,恢复尽可能多正确命令是

git checkout-index -af : this copies the version currently on the index (the one that you just put with git add . ) to the working directory git checkout-index -af :这会将当前在索引上的版本(您刚刚用git add .放置的版本)复制到工作目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM