ChatGPT解决这个技术问题 Extra ChatGPT

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

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?

Is this in the cloned repository, or in the original repository? Did the clone command output any errors?

C
Community

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.


I accidentally did a :w! in a :Gstatus (from fugitive.vim). This answer saved me a lot of hair pulling.
I know we don't like "me too" messages -- but "me too". Equivalent in Windows is erase /s .git\index, I needed a erase .git\index.lock too.
Hi, I had the same problem with find and replace but git reset tells me there are two pack files in .git/objects/pack/ that can't be accessed. Do you have an idea ?
wouldn't it be safer to use git reset --keep instead? In the Tower Git Cheat Sheet it is explained as: Reset your HEAD pointer to a previous commit and preserve uncommitted local changes
It didn't exist when I was writing this answer... Anyway git reset --keep is safer form of git reset --hard; git reset --mixed doesn't touch workdir at all.
h
hobs

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")

If you don't mind losing changes in .git/index, you can always delete it and recreate with git reset (without --hard!).
I broke it with # find ./ -type f -exec sed -i 's/Politician/Legislator/g' {} \; Doing what this answer recommends this would have not broken it in the first place, but the accepted answer repaired the damage that I did do. This is excellent prevention though.
@RyanMortensen You could try inverting your sed with something like find .git/ -type f -exec sed -i 's/Legislator/Politician/g' {} \; This might help if your .git/ is so corrupted that git reset won't work. Or maybe you want to restore your existing .git/index without deleting it. This will fail, of course, if your original code or index already had some "Legislator"s in it.
Thank you @hobs you saved me a lot of trouble - I solved it by inverting the sed by replacing my new_string with my old_string!
I refactored my whole project instead of the 'src' folder in IntelliJ and had this problem. This explains why I had such strange errors!
C
Cleiton Almeida

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.


This answer really resolve problem if you have some .git folders in vendor/ (e.x.)
'rm' is not recognized as an internal or external command, operable program or batch file!!!!!!!!!!!!!!!
G
Gav

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

git fsck --full

e
eskimwier

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

clone a new copy of the repo elsewhere 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.


That's brilliant :)
It's not really brilliant if you were in the middle of a merge, had created branches or had issued any commits since cloning, or any of a number of other scenarios... Cloning a new copy of the repo is hardly a solution and I daresay it smacks of impatience (best left when in a true pinch). It's much better to actually diagnose what's going on and repair the existing repo's index--that's usually relatively easy to do. Sometimes you can just rename the index file (or delete it, if you're sure you won't ever need it again) and let Git create a new one (using git-reset or git-checkout)..
D
DimaSan

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

This worked for me, Although It removed all added files from git. I had to run git add for those files
'rm' is not recognized as an internal or external command, operable program or batch file!!!!!!!!!!!!!!!!!!
j
jenming

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:

error: bad index file sha1 signature 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


E
Eric Leschinski

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.


Several other answers have already provided this information.
A
Ash

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.


M
Moein Qureshi

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


K
Kornel

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.


A
Astra Uvarova - Saturn's star

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.


Very dangerous because it will delete data like unpublished commits, tags and branches as well as stashes and the reflog.
Not sure about unpublished commits as I believe they are stored in .git folder and I copied .git folder. I didn't lose anything with this method. I don't know about stashes and reflog to make any comments on that.
You‘re correct, but maybe you should emphasize that you did a local clone. But my comment ist still true for stashes and reflog.
Okay, I don't have any experience on that comment anything further, however, it worked for me and some users might find it useful. There no need to downvote it.
F
FelixSFD
rm -f .git/index
git reset

More info at https://www.chris-shaw.com/blog/quick-fix-for-git-corrupt-index


Several other answers have already provided this information.
H
Herman Leus

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...


S
Shyamsundar

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


Don't put answer you don't know.