Recover from git reset --hard?
You cannot get back uncommitted changes in general.
Previously staged changes (git add
) should be recoverable from index objects, so if you did, use git fsck --lost-found
to locate the objects related to it. (This writes the objects to the .git/lost-found/
directory; from there you can use git show <filename>
to see the contents of each file.)
If not, the answer here would be: look at your backup. Perhaps your editor/IDE stores temp copies under /tmp or C:\TEMP and things like that.[1]
git reset HEAD@{1}
This will restore to the previous HEAD
[1] vim e.g. optionally stores persistent undo, eclipse IDE stores local history; such features might save your a**
answer from this SO
$ git reflog show
4b6cf8e (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: reset: moving to origin/master
295f07d HEAD@{1}: pull: Merge made by the 'recursive' strategy.
7c49ec7 HEAD@{2}: commit: restore dependencies to the User model
fa57f59 HEAD@{3}: commit: restore dependencies to the Profile model
3431936 HEAD@{4}: commit (amend): restore admin
033f5c0 HEAD@{5}: commit: restore admin
ecd2c1d HEAD@{6}: commit: re-enable settings app
# the commit the HEAD to be pointed to is 7c49ec7 (restore dependencies to the User model)
$ git reset HEAD@{2}
You got your day back! :)
I accidentally ran git reset --hard
on my repo today too while having uncommitted changes too today. To get it back, I ran git fsck --lost-found
, which wrote all unreferenced blobs to <path to repo>/.git/lost-found/
. Since the files were uncommitted, I found them in the other
directory within the <path to repo>/.git/lost-found/
. From there, I can see the uncommitted files using git show <filename>
, copy out the blobs, and rename them.
Note: This only works if you added the files you want to save to the index (using git add .
). If the files weren't in the index, they are lost.