How to resolve "Error: bad index – Fatal: index file corrupt" when using Git

GitCorruption

Git Problem Overview


After git init, I added and committed a few files, made some changes, added and committed. Set up the git daemon (running under Cygwin on WinXP) and cloned the repository once. Now, I get this error with the cloned repository:

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

Is there any way to fix this, other than getting a new copy of the repository?

Git Solutions


Solution 1 - Git

If the problem is with the index as the staging area for commits (i.e. .git/index), you can simply remove the index (make a backup copy if you want), and then restore index to version in the last commit:

On OSX/Linux/Windows(With Git bash):

rm -f .git/index
git reset

On Windows (with CMD and not git bash):

del .git\index
git reset

(The reset command above is the same as git reset --mixed HEAD)

You can alternatively use lower level plumbing git read-tree instead of git reset.


If the problem is with index for packfile, you can recover it using git index-pack.

Solution 2 - Git

You may have accidentally corrupted the .git/index file with a sed on your project root (refactoring perhaps?) with something like:

sed -ri -e "s/$SEACHPATTERN/$REPLACEMENTTEXT/g" $(grep -Elr "$SEARCHPATERN" "$PROJECTROOT")

to avoid this in the future, just ignore binary files with your grep/sed:

sed -ri -e "s/$SEACHPATTERN/$REPLACEMENTTEXT/g" $(grep -Elr --binary-files=without-match "$SEARCHPATERN" "$PROJECTROOT")

Solution 3 - Git

I had that problem, and I try ti fix with this:

rm -f .git/index
git reset

BUT it did not work. The solution? For some reason I had others .git folders in sub directories. I delete those .git folders (not the principal) and git reset again. Once they were deleted, everything worked again.

Solution 4 - Git

This sounds like a bad clone. You could try the following to get (possibly?) more information:

git fsck --full

Solution 5 - Git

Since the above solutions left me with continued problems, I used this dull solution:

  1. clone a new copy of the repo elsewhere
  2. copy the fresh .git directory into the (broken) repo that contained the changes I wanted to commit

Did the trick. Btw, I did a sed on the project root as @hobs guessed. Learned my lesson.

Solution 6 - Git

This worked for me. Although i'm curious of the reason I started getting the errors in the first place. When I logged out yesterday, it was fine. Log in this morning, it wasn't.

rm .git/index

git reset

Solution 7 - Git

Note for git submodule users - the solutions here will not work for you as-is.

Let's say you have a parent repository called dev, for example, and your submodule repository is called api.

if you are inside of api and you get the error mentioned in this question:

fatal: index file corrupt```

The `index` file will NOT be inside of a `.git` folder.  In fact, the `.git` won't even be a folder - it will will be a text document with the location of the real .git data for this repository. Likely something like this:

```~/dev/api $ cat .git
gitdir: ../.git/modules/api```

So, instead of `rm -f .git/index`, you will need to do this:

```rm -f ../.git/modules/api/index
git reset```

or, more generally,


```rm -f ../.git/modules/INSERT_YOUR_REPO_NAME_HERE/index
git reset```

Solution 8 - Git

This issue can occur when there is a .git directory underneath one of the subdirectories. To fix it, check if there are other .git directories there, and remove them and try again.

Solution 9 - Git

None of the existing answers worked for me.

I was using worktrees, so there is no .git folder.

You'll need to go back to your main repo. Inside that, delete .git/worktrees//index

Then run git reset as per other answers.

Solution 10 - Git

Cloning remote repo and replacing the .git folder from it to problematic local directory solved the issue.

Solution 11 - Git

A repo may seem corrupted if you mix different git versions.

Local repositories touched by new git versions aren't backwards-compatible with old git versions. New git repos look corrupted to old git versions (in my case git 2.28 broke repo for git 2.11).

Updating old git version may solve the problem.

Solution 12 - Git

I did a simple trick. I clone the repo to a new folder. Copied the .git folder from the new folder to repo's old folder, replacing .git there.

Solution 13 - Git

Solution 14 - Git

This is ridiculous but I just have rebooted my machine (mac) and the problem was gone like it has never happened. I hate to sound like a support guy...

Solution 15 - Git

You can also try for restore to previous version of the file (if you are using windows os)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNumber8View Question on Stackoverflow
Solution 1 - GitJakub NarębskiView Answer on Stackoverflow
Solution 2 - GithobsView Answer on Stackoverflow
Solution 3 - GitCleiton AlmeidaView Answer on Stackoverflow
Solution 4 - GitGavView Answer on Stackoverflow
Solution 5 - GiteskimwierView Answer on Stackoverflow
Solution 6 - GitEightyView Answer on Stackoverflow
Solution 7 - GitjenmingView Answer on Stackoverflow
Solution 8 - GitNick KuijpersView Answer on Stackoverflow
Solution 9 - GitAshView Answer on Stackoverflow
Solution 10 - GitMoein QureshiView Answer on Stackoverflow
Solution 11 - GitKornelView Answer on Stackoverflow
Solution 12 - GitAstra Uvarova - Saturn's starView Answer on Stackoverflow
Solution 13 - GitChristopher ShawView Answer on Stackoverflow
Solution 14 - GitHerman LeusView Answer on Stackoverflow
Solution 15 - GitShyamsundarView Answer on Stackoverflow