lkky7
lkky7

Reputation: 567

How to recover git repository accidentally deleted before committing anything

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?

Upvotes: 1

Views: 165

Answers (1)

torek
torek

Reputation: 487725

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 :-) )

Upvotes: 3

Related Questions