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.
this soln didnt work for me. I'd the same problem. I tried to push my local folders (250 folders, 950 files) to github after creating a empty repository. followed web examples, this is my first time with github. git terminal keot throwing error that few files are above 100MB, I have added, committed and I got this error when I pushed. I moved the three files to another folder. and it still threw. I realised I have to remove them from the commit. I used reset. added, committed, tried pushing, no matter how many times, i tried those 3 alrge files appeared though they didnt even exist in my folders. after 1 hr of various commands, all of a sudden I lost all my files. they were not in windows recycle. after some research found out it is in .git folder under objects but not in original format. How do I recover now? I dont have a backup and these files are not pushed to github yet. if I cannot recover, I will loose close to 950 files. please help