0

I did a commit with a lot of work and accidentally a folder of images which was huge. I then did a git reset --hard HEAD^ and thought it would just remove the commit. But it turned out it also removed all the work I had done ... huge bummer.

Now, I've read hundreds of posts about recovering from that exact accident. But no matter what I try, I simply cannot recover all the edited files and folders I've created during the last days.

HOW, can I make git give me back the lost files?

OK. Obviously clearly overworked here ... what I did was:

git add .

And then after seeing my mistake I did a:

git reset --hard HEAD^

Yes, totally screwing around here.

How do I recover from this situation?

  • 2
    Possible duplicate of [How can I recover a lost commit in Git?](https://stackoverflow.com/questions/10099258/how-can-i-recover-a-lost-commit-in-git) – D. Ben Knoble Jul 10 '19 at 22:34
  • Two questions - was the work you had done part of any commit or uncommitted work? Have you pushed the branch? – Eric H Jul 10 '19 at 22:37
  • The problem is I didn't commit my work ... so no, those posts do not answer my question. –  Jul 10 '19 at 22:46
  • 1
    See https://stackoverflow.com/questions/5788037/recover-from-git-reset-hard/5788069#5788069, in particular [this answer](https://stackoverflow.com/a/6780036/1256452). – torek Jul 10 '19 at 22:50
  • 1
    Possible duplicate of [Undo git reset --hard with uncommitted files in the staging area](https://stackoverflow.com/questions/7374069/undo-git-reset-hard-with-uncommitted-files-in-the-staging-area) – melpomene Jul 10 '19 at 22:52
  • I did a git add . before ... but HOW do I get the files back? That's pretty unclear in the answer. –  Jul 10 '19 at 23:10
  • If I run: git fsck --lost-found I get 1945 lines of : dangling blob dcffe67dc2cba34165b0942eff58adb7815090f8 –  Jul 10 '19 at 23:12
  • 1
    _always commit early and often_. If it’s not committed then check your IDE. – evolutionxbox Jul 10 '19 at 23:12
  • did you try "git reflog" – jo_ Jul 11 '19 at 14:36

1 Answers1

2

After git fsck --lost-found tells you about all the "dangling blob"s it saved, look in .git/fsck/lost-found/other. There are many files in here, with completely useless names (actually just hash IDs), but with the contents of the files that you had git add-ed that were not already in your repository.

You can either go through the files as they are here, or copy or move them out to a temporary area elsewhere. You must look at each file, figure out what name it should have or whether you want to keep it at all, and if so, give it a better name.

The original file names are not saved anywhere in Git (they were in your index, but git reset --hard overwrote that index with the current index). (They would have been saved beneath a commit, had you made a commit.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • So that's 1.945 (one thousand nine hundred and fourty five) files that I have to look into manually!? –  Jul 10 '19 at 23:18
  • 1
    Yep. It's kind of terrible. Most of them are probably those image files that you didn't want, and it's probably easy to automate identifying them and moving them out of the way, leaving only a few hundred files to check. – torek Jul 10 '19 at 23:19
  • 1
    Thanks for confirming that. But I think I'd rather learn the lesson and recreate the work all over. I've heard it's faster the second time you have to do it ; ) –  Jul 10 '19 at 23:41