undo git reset --hard, how to do it?

263 views Asked by At

My uncommitted files deleted by git reset --hard command. How can I recover? I tried git reflog etc but it stays at 0%.

Checking object directories: 100% (256/256), done.ories:   0% (1/256)

enter image description here

2

There are 2 answers

0
VonC On

For staged file, you can check the output in git fsck, but for private files, as in not even staged (git add), then those files are gone.
git reflog would only help to restore past commits.

Double-check with:

0
dejanualex On

If you have staged your file, you can try git fsck --lost-found then take a look into .git/lost-found/other and using git show <SHA> check what you can recover

So I've recreated your scenario: (created new_file then stage it, and git reset --hard afterwards)

enter image description here

Checked the content of .git/lost-found/other enter image description here

Then recover the content git show <SHA>: enter image description here

Fortunately in my case I had a small number of commits so it was easy to find it.