11

I made a terrible mistake and execute "git reset --hard HEAD", all day's the local modification lost, how can i recover it?

Thanks millions

LiangWang
  • 8,038
  • 8
  • 41
  • 54
  • 1
    Possible duplicate of [Recovering added file after doing git reset --hard HEAD^](http://stackoverflow.com/q/1108853/456814). –  May 19 '14 at 03:42

6 Answers6

30

You can use git reflog. It will show HEAD history. You can pick the hash that represents the HEAD status before the git reset --hard and use this hash in another git reset --hard .

William Seiti Mizuta
  • 7,669
  • 3
  • 31
  • 23
17

If you didn't already commit your local changes (or at least stage them via git add, they're gone. git reset --hard is a destructive operation for uncommitted changes.

If you did happen to stage them, but didn't commit them, try git fsck --lost-found and then search through the contents of .git/lost-found - it will contain all of the objects that aren't referenced by a known commit, and may include versions of files that were staged.

Amber
  • 507,862
  • 82
  • 626
  • 550
17

First run:

git reflog

It will show history of your HEAD pointer. Then select sha-code of a necessary state from first column. I think it will be near HEAD@{1} if you made just git reset --hard once. And then

git merge SHA_CODE

Bingo!

More examples you can find here: http://www.programblings.com/2008/06/07/the-illustrated-guide-to-recovering-lost-commits-with-git/

4

You can recover anything you git added, with git fsck --lost-found and poke around in .git/lost-found. find .git/objects -type f | xargs ls -lt | sed 60q will give you the last 60 things to get added to the repo, that'll help.

Anything you didn't git add is gone as surely as if you'd deleted it yourself.

jthill
  • 55,082
  • 5
  • 77
  • 137
1

If your IDE has something like a "Local history" (Eclipse has this, IDEA also AFAIK), then maybe you can recover your changes this way.

dunni
  • 43,386
  • 10
  • 104
  • 99
0

I recovered the files not "git add"-ed with my Intelliji "local history".