2

I just made a huge accident. I wanted to undo a local commit and I ran git reset --hard HEAD~1. However I had many uncommited files (about two days of work).

All the uncommited changes have been reset too.

Help!

How do I undo a git reset --hard HEAD~1?

6

2 Answers 2

3

You can revert ant code you have in your local git folder, whether its was committed or not.

What can be recovered:

  • All commited changes
  • All added items (git add without git commit)
  • All loose blobs (dangling files)

What can't be recovered:

  • Deleted files from the local file system. (You can recover those by using 3rd party tool which scan your drive and recover them)

How to recover:
First run git reflog ro git log -g to verify what did you clean. Then checkout the commit that you wish to recover.
Now you have to recover added but uncommited files. To find those files run git fsck and it will print out the dangling files. use git cat-file -p <SHA1> to print them out to screen and then recover them.

git log -g will display the reflog entry for each of your commit.

Good luck.

0

Claim down! You can run "git reflog" to find your commit logs, and then run "git reset --hard ". Hope it works.

3
  • I'm holding off on running anything. The question is whether I can get back the uncommited changes. Reading this page stackoverflow.com/questions/7374069/…
    – bodacydo
    Commented Mar 3, 2015 at 9:32
  • Sorry. I don't know how you lost your uncommited changes. Do they stay in your working directory?
    – jansesun
    Commented Mar 3, 2015 at 9:48
  • 1
    @bodacydo no need to hold, make a full copy instead (including .git directory) and try something in copy, not in original. But no, unstaged changes are very unlikely to be recovered. Think twice before adding --hard, name suggests it is dangerous.
    – keltar
    Commented Mar 3, 2015 at 9:49

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