This script will restore all the objects in your .git/objects folder.
To start, you must identify what the "root object" is. This will be a tree object that lists out the files that are in the root of your git repository.
Once you have that object identified, this script will recursively walk through the tree and restore all the files that git knows/knew about.
background
I started working on a new project and got quite a bit of work done locally before initializing a git repo or creating any commits.
When I decided it was time to commit, I ran git init && git add . && git commit. At this point, I realized that there were some files I wanted to add to the .gitignore, so I cancelled the commit and composed my .gitignore file. At this point, I decided I'd re-add everything (now that I was ignoring stuff I didn't need) and ran git reset --hard.
YIKES
This wiped out all the code I had written.
No attempts to recover would work (git reflog was empty, git fsck couldn't find any objects, git rev-log had nothing). All I had what was left of the .git/objects directory.
After some reading and understanding of how the git object data is stored and written, it became clear that I could recover my data by parsing these files by hand.
The attached script does exactly that.
What a Monday.
@3d-illusions
You can use this bash script to output a list of the objects that are trees (directories) and the files they point to:
this will output (potentially) a lot of stuff. You'll want to look through it to find the object that contains the files on the root of the directory.
Here's an example of one of mine:
The root object identifier in this case is:
1e9fd13ba91a602176ec2ed1b7616465d93c9abbWorth noting, the directories listed in
./git/objectsare the prefix for each object within those directories. So, in the case of the root object, above, it's actually located in:.git/objects/1e/9fd13ba91a602176ec2ed1b7616465d93c9abb. This is worth making note of, if you try to run thegit cat-filecommands directly.Hope this helps.