0

I need help, I had files that were not added to the repo yet. I did

 git add this.php
 git add file.php

then I decided I wanted to unstage them so I did

 git reset --hard HEAD

Now these files are gone. How can I recover these files?

2 Answers 2

5

Luckily for you, you added those file to the index before nuking them from your working copy. In this case they are already contained in Git's object database. (First create a backup, just in case you mess something up).

Run git fsck (maybe with --full flag) and look out for "dangling blobs". Then use git show $hash to display the contents of the blobs. If you find your files (there might be several similar versions), use git show $hash > path/to/file to write them to disk again.

1
  • Wow, what a relief. This works. I was using fsck without --full so the blobs were not showing up for those files. I need to learn
    – Angelo
    Commented Sep 4, 2014 at 16:09
0

You cannot recover the files after a hard reset without having committed them.

You should have rather done a soft/mixed reset, which will just unstage the files. You can see the same suggestion from git when you do a git status after staging the files.

2
  • 1
    OP added them before calling reset --hard so they are already part of Git's object database and can be recovered with git fsck. If he hadn't, then the files would be lost forever.
    – knittl
    Commented Sep 4, 2014 at 15:58
  • Ah, gotcha. Learnt it now! :-)
    – gravetii
    Commented Sep 4, 2014 at 16:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.