788

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?

Simon East
  • 55,742
  • 17
  • 139
  • 133
Number8
  • 12,322
  • 10
  • 44
  • 69
  • Is this in the cloned repository, or in the original repository? Did the clone command output any errors? – CB Bailey Jul 12 '09 at 11:27

18 Answers18

1644

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.

Community
  • 1
  • 1
Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
  • 31
    I accidentally did a `:w!` in a `:Gstatus` (from fugitive.vim). This answer saved me a lot of hair pulling. – Laurence Gonsalves Feb 29 '12 at 17:46
  • 6
    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. – Jeremy McGee Jun 15 '12 at 07:23
  • I had to use rm .git/index as I received an ambiguous warning using -f – Nicholas Murray Jul 16 '12 at 21:43
  • 1
    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 ? – epsilones Jan 28 '13 at 16:44
  • 14
    wouldn't it be safer to use `git reset --keep` instead? In the [Tower Git Cheat Sheet](http://www.git-tower.com/files/cheatsheet/Git_Cheat_Sheet_grey.pdf) it is explained as: _Reset your HEAD pointer to a previous commit and preserve uncommitted local changes_ – Pjetr Jun 13 '13 at 13:30
  • 15
    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. – Jakub Narębski Jun 13 '13 at 19:15
  • Nothing changed for me... version `1.9.1` – Vitaly Zdanevich Nov 10 '15 at 06:30
  • How can you tell if it's a problem with the *Staging Area* or the *Packfile*? – Clay Nichols Mar 23 '17 at 16:45
  • Are those commands issued from the Windows CMD prompt or from the BASH Git command prompt? – Clay Nichols Mar 23 '17 at 19:37
  • Followed first instructions Windows. Did not resolve problem. Followed second instructions but got a response (for git index-pack) with usage info. Apparently Git needs some parameter supplied. Tried 'git reset --keep' and got error : You do not have a valid HEAD. fatal: could not reset index file to revision HEAD. Now my "view log" doesn't even work. – Clay Nichols Mar 23 '17 at 19:44
  • @ClayNichols: neither of those instructions affects HEAD, so your problem is unrelated to the index. If `git log ` works, or `git show ` works, or if it is something in `.git/logs/` you should be able to recover at least some of history... and there is also `git fsck --lost-found`. – Jakub Narębski Mar 23 '17 at 21:18
  • worked perfectly for me. tried those commands in git shell of gitHub – SKK Sep 25 '17 at 13:58
  • For me on Windows it was rm .git/index and after git reset --keep – Levon Petrosyan Dec 08 '17 at 11:17
  • I had to do a `git checkout .` on the repository root after because it kept saying that some files were modified (`git diff`was of no help because it treated the files as binary). – Gabriel Hautclocq Oct 23 '18 at 15:21
  • The `git reset` fails with `fatal: loose object (stored in .git/objects/b2/) is corrupt`. – aroth Feb 11 '19 at 00:32
  • Folks doubting if there changes will be deleted , no they will be not after doing "git reset" . – infiniteLearner Jan 06 '20 at 11:06
  • I manually deleted the index file under the.git folder and then did "git reset" and it worked for me. – Usman Farooq Jun 06 '20 at 23:24
  • Got this error on VS code after the update and just used the CMD to run the commands and successfully reset the index – NJENGAH Jul 05 '20 at 22:30
  • 1
    The fun in getting to the same helpful answer again, and see that I already upvoted last time =] – Yitzchak Aug 04 '20 at 07:36
  • @Pjetr `git reset --keep` also throws `fatal: index file corrupt` – Pathros Aug 18 '20 at 03:15
  • 'rm' is not recognized as an internal or external command, operable program or batch file!!!!!!!!!!!!!!!!!!!! – Dyno Cris Jun 09 '21 at 23:29
77

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")
hobs
  • 18,473
  • 10
  • 83
  • 106
  • 7
    If you don't mind losing changes in `.git/index`, you can always delete it and recreate with `git reset` (without `--hard`!). – Jakub Narębski Mar 01 '12 at 12:24
  • 2
    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. – Ryan Mortensen Mar 11 '15 at 01:34
  • 1
    @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. – hobs Feb 22 '18 at 19:30
  • 2
    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`! – tsveti_iko Jul 11 '18 at 08:20
  • 1
    I refactored my whole project instead of the 'src' folder in IntelliJ and had this problem. This explains why I had such strange errors! – Michael Jan 30 '19 at 19:36
30

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.

Cleiton Almeida
  • 452
  • 4
  • 8
20

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

git fsck --full
Gav
  • 11,062
  • 7
  • 33
  • 35
17

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.

eskimwier
  • 1,037
  • 13
  • 16
  • That's brilliant :) – Jeremy Belolo Feb 21 '17 at 11:06
  • 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).. – Jazimov May 01 '17 at 03:10
  • Only solution of all the suggestions that actually worked without major extra steps! Thanks. – vik Jun 25 '23 at 07:17
15

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
DimaSan
  • 12,264
  • 11
  • 65
  • 75
Eighty
  • 159
  • 1
  • 3
8

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

jenming
  • 809
  • 7
  • 8
5

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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Nick Kuijpers
  • 267
  • 3
  • 3
2

This issue is occoured due to changes in git file or brach. Then you need to run this command on your cmd.

  1. del .git\index
  2. git reset --hard

After run 2 commands your Git Repo is working well

1

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/<name_of_tree>/index

Then run git reset as per other answers.

Ash
  • 1,956
  • 1
  • 14
  • 15
1

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

1

I committed my changes and suddenly my laptop went off due to battery issue and then I got this fatal index corrupt error and Desktop github couldn't locate this git repo. So I ran below mentioned commands using git-bash and everything went back fine.

rm -f .git/index
git reset
Ahsan Khan
  • 678
  • 9
  • 12
0

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.

Kornel
  • 97,764
  • 37
  • 219
  • 309
0

On Windows PowerShell, it should be

rm -Force .git/index
git reset
ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
0

I had the same error and attempted to fix it as described in the accepted answer. However, immediately after resetting my index in .git/index it became corrupted again.

The problem was, that I was using git bisect yesterday to find a bug, but forgot to run git bisect reset to clean up afterwards at the end of the day. Today as I started making changes git got confused, as it was still in bisect mode and I was attempting to apply changes. I also couldn't just run git bisect reset, because there were already changes.

To fix this problem run the following in the git bash in your root directory:

git stash -m <message>
git bisect reset        # You are now most likely in detached head mode
git checkout <branch>
git stash apply         # If you have multiple stashes, make sure 
                          to apply the correct one

Note that there is no need to remove .git/index and reset the index in this case!

Liqs
  • 137
  • 1
  • 9
-2

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. – Koraktor May 28 '20 at 07:42
  • 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. – Astra Uvarova - Saturn's star May 30 '20 at 21:20
  • You‘re correct, but maybe you should emphasize that you did a local clone. But my comment ist still true for stashes and reflog. – Koraktor May 31 '20 at 09:15
  • 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. – Astra Uvarova - Saturn's star Jun 02 '20 at 02:56
-4
rm -f .git/index
git reset

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

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Christopher Shaw
  • 763
  • 6
  • 19
-9

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