I use git as my version control system for all of my sites. It helps to reduce disk usage by occasionally running git gc. Last night when I went to run git gc on one of my sites, I received a message I had never seen before telling me that my git repo was corrupt. Corrupt? Oh noes!
So I searched the internet and discovered I could run git fsck –full to figure out what the problem was:
$ git fsck --full broken link from tree e79ab5efffca93a784afdb9cef73801eb1fa9db4 to tree fb9d707c76dde3f8cb672d1c07ca046615175587 dangling tree 1003e43ee3725b0482193aea9d82ca527c2903cc dangling blob 361211b552b7910299e63b6627944ff72b0577eb dangling blob 8e79df8dbe87431fbec8f25a4c6527fabe4d9e6f dangling blob 9b3a891e5094920f743c36a49d1647d9aad2b2f0 missing tree fb9d707c76dde3f8cb672d1c07ca046615175587
After digging around the internet, I discovered that my repo was missing a tree, and that I could get it out of a cloned copy of the repo, assuming the cloned copy wasn’t corrupt, too.
I created a new empty repo:
$ mkdir new-repo $ cd new-repo $ git init
Next, I copied my pack file from the good repo to the new empty repo (there was only one in my pack folder):
$ cd /www/path/to/good/repo $ cp .git/objects/pack/pack-c376dc3b9d2ce5f4d9ab9269d29ddc82f6f6a822.pack /path/to/new-repo
I unpacked the pack file to see if I could find the missing tree:
$ cd /path/to/new-repo $ git unpack-objects < pack-c376dc3b9d2ce5f4d9ab9269d29ddc82f6f6a822.pack
My missing tree is fb9d707c76dde3f8cb672d1c07ca046615175587, so I looked to see if my file was there:
$ cd .git/objects/fb/ $ ls -l
9d707c76dde3f8cb672d1c07ca046615175587 showed up in the list, so yay! My missing tree was there! Now to copy it to the corrupt repo (my corrupt repo was a bare repo, so no .git in the path):
$ mkdir -p /path/to/corrupt/repo/objects/fb $ cp /path/to/new-repo/.git/objects/fb/9d707c76dde3f8cb672d1c07ca046615175587 /path/to/corrupt/repo/objects/fb/9d707c76dde3f8cb672d1c07ca046615175587
That was it for me! I ran git gc and everything worked without a problem.
Hopefully this helps someone, or at least will help me remember what the heck to do next time my repo gets corrupted.