I'm a programmer specialising in performant and scalable systems using PHP and Ruby and cooking


Published:
On Git

My git index is corrupt....

If, like me, you have come across this error and wondered how to fix it without recreating your whole project then don't fear, it's actually really rather simple.

I've seen this error arise quite frequently for users on windows machines working over samba to a linux box hosting the project files, perhaps this is something to do with the way Samba transfers files, voodoo or multiple users updating the file at the same time. In my opinion I think it's being caused by multiple users make conflicting updates to the index.

Anyway here is the error:

$ git status
error: bad index file sha1 signature  
fatal: index file corrupt  

This is basically saying that the file (.git/index) has become unreadable. This well block any operation that involves this file, which is pretty much everything.

The index is a binary file (generally kept in .git/index) containing a sorted list of path names, each with permissions and the SHA1 of a blob object;

Fixing this without trashing your working copy, or messing with your changes is as simple as removing the broken index file and recreating it. Fortunately the index is basically only a cache of the information required for git to work within your directory so isn't all that important.

First make sure you are within your working copies root and then run the following commands:

$ rm -f .git/index
$ git reset

Simple as that your index will be recreated from you current object data (HEAD) and your working directory. Note on large projects this may take some time.