How to mess up git

August 5, 2009


So you think you know git… today I found out something “funny”. In a git repository:

$ git log
$ fatal: object 1fcc8de9361c56e538ff35d8cc4b07a9c95b7bf3 is corrupted

Okay, WTF? Lets look in the .git directory:

$ cd .git/objects/1f
$ ls -l
total 4
-r--r--r-- 1 root root 1057 2009-06-23 19:03 03db070bcb47bff3f8106f2ec7028b3496aaa8
-r--r--r-- 1 root root    0 2009-08-03 21:41 cc8de9361c56e538ff35d8cc4b07a9c95b7bf3

Ah, 0 bytes, that explains it (probably due to the weird reboot of last night and ext4).

Okay, lets fix this:

$ mv cc8de9361c56e538ff35d8cc4b07a9c95b7bf3 /tmp
$ git status
# On branch master
fatal: bad object HEAD

Hmmm, not good, let look in .git/refs/heads

$ cd .git/refs/heads
$ ls -l
total 0

Hmm, nothing, peeking in another git repo says me that you need a file master here which holds and object ref (a SHA1 string). Lets create one. First find the latest hash:

$ find . -mtime -1 -print

Okay, lets put that in the master file:

$ cat > master

And git status came back to live - so something is working again :) Now try to add something:

$ git add <somefile>
fatal: bad tree-ish HEAD

Oh oh… Let “fix” the archive some more:

$ git gc

That did the trick, my git repository is now fubar

$ git co
fatal: You are on a branch yet to be born


$ git branch master
fatal: A branch named 'master' already exists.

Hmmm, really nice.

$ git branch
error: branch 'master' does not point at a commit

$ git co master
fatal: reference is not a tree: master

The only way to fix this for now was to zapp the entire .git directory:

rm -rf .git
git init

And create a brand new repository.