1

I decided to try GitHub for the first time and already managed to delete all my files in my most important project while playing around. Here is my console history:

git init
git remote add origin https://github.com/...
git add .
git config --global user.email (...)
git config --global user.name (...)
git reset --hard   // amazing move by me

I searched for many ways to recover this. One recommended was: git fsck --lost-found but it only returns:

missing tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
dangling tree 3178c349021f733a9fa7fa0fabd2ac34f8841bdd

and no blobs. So is there no hope to recover it?

lkky7
  • 557
  • 1
  • 4
  • 10
  • `git reset --hard`? So you decided to pull out the big guns, huh? well.... the blobs don't show up because they are being pointed to by those trees (an educated guess). – eftshift0 Sep 22 '18 at 19:46
  • When i type `git show 3178c` it shows me the list of deleted folders and files. What do I do next? – lkky7 Sep 22 '18 at 19:49
  • Nothing comes to mind, unfortunately. Maybe a file rescue program? From the tone, I assume this was your only copy of the program and you didn't make a backup before playing with Git? – Matt Runion Sep 22 '18 at 19:50
  • 1
    If you have identified the tree that you want to keep, you might want to create a revision pointing to it? It's a little hackish but possible. `git commit-tree`. So, something like `git commit-tree 3178c -m "Recovering my tree"`. That will provide you with the ID of the revision pointing to that tree (the revision you just created) and then you can checkout providing that id. If everything looks correct, you could create a branch on that revision and you are done. – eftshift0 Sep 22 '18 at 20:00
  • Commit early and often. – evolutionxbox Sep 23 '18 at 10:30

1 Answers1

3

That's odd, because missing tree and dangling tree cannot occur unless there are or were commits made. It's git write-tree that builds tree objects from the index (or git hash-object -w -t tree but this is hard to use). (Well, the missing tree is the empty tree—I thought that hash ID sounded familiar!—so that's a bit less odd.)

Still, however you got to this point, the dangling tree object is probably what has your blob hash IDs. Use git show or git ls-tree -r on it to get file names and blob hash IDs, then use git show or git cat-file -p on each blob ID to get the file's contents, and store those contents under the name you find in the tree.

Or, you can use eftshift0's trick: turn the dangling tree into the tree of a commit. That's even better / more convenient. (eftshift0 should turn this into an answer, which you should accept :-) )

torek
  • 448,244
  • 59
  • 642
  • 775
  • By the way... if a commit was ever made that has that tree, you might just check `git reflog` to get that commit back from history. – eftshift0 Sep 22 '18 at 20:09
  • And that's the hero right there... `git ls-tree -r` was the key command to help me. Other didn't do anything. I'll be more careful in the future, thanks. – lkky7 Sep 22 '18 at 20:10