0

I just made a git reset--hard command by accident. Can this git action somehow be reverted? I want to obtain back the uncommitted changes. I am using git from the command line.

Can I obtain the code from a still running tomcat/grails instance?

Jacob
  • 3,580
  • 22
  • 82
  • 146

3 Answers3

2

If the changes were never committed, the answer is no, you can't.

If you did a hard reset and lost committed changes, you can get them back by inspecting the reflog.

musiKk
  • 14,751
  • 4
  • 55
  • 82
1

If the changes were not committed or staged, the answer is no.

If you committed changes, you can find back since the commit objects are there.

If you staged your changes (using git add), you can still find back your changes, because the blob objects for your changes have been generated.

First of all, please copy your whole git repository as a backup.

Just for investigation, you can ls -l -t .git/objects to find these objects by their creating time. Each object is stored with the first 2 characters of the object id as folder name, and the left part as file name. For example, 78/981922613b2afb6025042ff6bd878ac1994e85'. And you can usegit cat-file` to get it's content.

git cat-file -p 78981922613b2afb6025042ff6bd878ac1994e85

Or just with the short name (first several characters of the id).

git cat-file -p 789819

To do recovery, I just found an answer for you. Please refer to Undo git reset --hard with uncommitted files in the staging area.

Community
  • 1
  • 1
Landys
  • 7,169
  • 3
  • 25
  • 34
  • I did a git add where can I find the blob? – Jacob Sep 03 '14 at 10:46
  • Generally, it's in `.git/objects`, you may find it by the creating date. Just wait for a few minutes, I try and let you know. – Landys Sep 03 '14 at 10:48
  • This is all binary data, how can I view it – Jacob Sep 03 '14 at 10:59
  • Just update the answer. They are actually the content of your file with some header information that is zip compacted. And these objects are called unreachable or dangling objects. You can also use `git fsck` to find them. – Landys Sep 03 '14 at 11:10
  • I always get a response "Not a valid object name" – Jacob Sep 03 '14 at 11:13
  • Are you sure the id is correct? It should combine the folder and file name. Or you can use `git fsck --cache --unreachable` to get the unreachable and dangling blob. Then use `git cat-file` to have try. – Landys Sep 03 '14 at 11:16
  • Sorry. I missed two folder charachters at the start. You rock! Saved my day. – Jacob Sep 03 '14 at 11:20
0

There is no way to get back uncommitted changes until you had not make a temporary patch before "git reset --hard" now you can "git pull" to synchronous.

But there is some tricky way you can get your uncommitted changes only if you have did some following commands before "Git reset --hard" : 1. git diff and to exit from that if you had press ctrl+z

then you can get that back by command "fg". because ctrl+z will make the git diff command running at background. if you had not done above before "Git reset --hard", sorry you have to write changes again. better luck.