0

After git add . I ran 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 --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:

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(R.R/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.

Carlo Bellettini
  • 1,130
  • 11
  • 20
Rahul
  • 115
  • 10

2 Answers2

2

I tried to reproduce what you did:

1) git add .

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

but the correct command were as indicated in my previous answer git checkout-index -af (but this is useless after the reset)

at this point you gave correctly the git fsck --lost-found, my suggestion was equivalent with lower level commands (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)? Perhaps in some hidden directory created by a IDE?

Carlo Bellettini
  • 1,130
  • 11
  • 20
  • Yes there were .pyc files but also also .py files. Where are those? – Rahul Dec 11 '17 at 13:08
  • Sorry, my bad after closely looking each and every file, I eventually found all the files. It will be a headache now to copy these back. :) Thanks very much for your help – Rahul Dec 11 '17 at 13:14
0

1) rm -r -f . should abort without effect probably you did rm -r -f * and it does not delete the .git directory

2) this is confirmed by the fact that the command git fsck --lost-found did not abort

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

Carlo Bellettini
  • 1,130
  • 11
  • 20