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.
Try each unique commit hash in any order.