"Index file corrupt" using git

5

Use git for version control.

But I have started receiving the following error message when I try to do any operation in the repository:

fatal: index file corrupt

This means that it can not operate or do anything with the code.

Why does this error occur? What can I do to solve it?

    
asked by Alvaro Montoro 02.03.2017 в 17:05
source

1 answer

5

Good morning.

This could happen when some files in the .git folder, specifically the index is damaged.

to fix it try (in linux):

rm -f .git/index
git reset --keep

Explanation

To reproduce the error:

[email protected]:/var/www/repo$ ls -lha .git/               # Ver los archivos de .git/
[... otros archivos ...]
-rw-r--r--   1 arie arie  175K mar  2 13:18 index   # Fichero correcto

[email protected]:/var/www/repo$ git add .                   # Este funciona!
[email protected]:/var/www/repo$ truncate -s 1000 .git/index # Echamos a perder :(
[email protected]:/var/www/repo$ git add .                   # Error!
error: bad index file sha1 signature
fatal: index file corrupt

The Index file

  

The staging area is a file, usually contained in your Git directory, that stores information about what will go into your next commit. It's sometimes referred to as the "index", but it's also common to refer to it as the staging area. The Three States - Git Basics

The "index", "stagging area" or "preparation area" is a file that contains the capture of the current working tree that will be included in the next "commit".

The git reset [< mode >] [< commit >]

command
  

This form resets the current branch head to < commit > and possibly updates the index (resetting it to the tree of < commit >) and the working tree depending on < mode & gt ;. Git reset - git documentation

Git reset relocates the "head" pointer to the selected commit between < > , along with this updates the "index" file. If paramatro <commit> is not provided, by default it is HEAD . For <mode> is --mixed .

option - keep

  

Resets index entries and updates files in the working tree that are different between < commit > and HEAD. If a file that is different between < commit > and HEAD has local changes, reset is aborted. Option --keep - Git Documentation

You can read this article on the git-scm blog (in English) to have much more information.

    
answered by 02.03.2017 / 17:10
source