How to fix error: bad index file sha1 signature fatal: index file corrupt in Git

GitINTERMEDIATEHIGHUpdated Dec 6, 2025ยท 219 views

This error occurs when Git's index file (.git/index) becomes corrupted, typically due to power loss, system crashes, or abrupt process termination during Git operations. The fix involves removing the corrupted index and regenerating it from HEAD.

Quick fix

Fixes error: bad index file sha1 signature fatal: index file corrupt

# Check the error message
git status

# Examine the index file
ls -la .git/index

# Check if the file is empty or suspiciously small
# A normal index is typically at least a few KB
file .git/index
  1. Verify the corruption
  2. Back up any uncommitted changes (if possible)
  3. Remove the corrupt index file
  4. Regenerate the index from HEAD
git โ€” error
# Check the error message
git status
# Examine the index file
ls -la .git/index
# Check if the file is empty or suspiciously small
# A normal index is typically at least a few KB
file .git/index
error: bad index file sha1 signature
fatal: index file corrupt

What this error means

The "index file corrupt" or "bad index file sha1 signature" error indicates that Git's staging area has become unreadable. The index file, located at `.git/index`, is a binary file that tracks the state of your working directory and what's staged for the next commit. When Git detects that this file's checksum doesn't match its contents, it refuses to proceed to protect your repository from further damage. The index file serves as Git's cache of your working tree state. It stores metadata about every tracked file including paths, file modes, and SHA-1 hashes of the file contents. Git uses this information to quickly determine what has changed between your working directory and the last commit, and what's currently staged. The "inode cache" referenced in some error messages refers to the filesystem metadata that Git caches for performance. Corruption typically happens when the index file is only partially written. Git writes to the index atomically by creating a new file and renaming it, but if the system crashes at the wrong moment, or if an external process interferes with the write, the resulting file can be incomplete or contain invalid data. The SHA-1 checksum at the end of the index file fails to match, and Git reports corruption.

Typical symptoms

  • Git commands fail with 'error: bad index file sha1 signature'
  • Running 'git status', 'git add', or 'git commit' produces 'fatal: index file corrupt'
  • The error appeared suddenly after a system crash, power outage, or forced shutdown
  • All Git operations in the repository fail immediately with the same error
  • The .git/index file exists but may be smaller than expected or zero bytes
  • CI/CD pipelines fail with index corruption after container or VM interruption
  • The error reappears immediately after attempting to fix it (indicates ongoing filesystem or process issue)

Common causes

  1. Power loss or system crash during an active Git operation (most common cause)
  2. Abrupt process termination (kill -9, sudden reboot, container shutdown) while Git was writing to the index
  3. Antivirus or security software scanning and locking .git/index mid-write
  4. Filesystem corruption or failing storage device (corrupted disk sectors, bad RAM)
  5. NFS or network filesystem glitches causing partial writes or stale file handles
  6. External backup software or file sync tools (Dropbox, OneDrive) modifying files during Git operations
  7. Git version mismatch where a repository created with a newer Git version has an incompatible index format
  8. Disk space exhaustion during index write causing truncated file

How to fix "error: bad index file sha1 signature fatal: index file corrupt"

1Verify the corruption

First, confirm that the index is actually corrupt and understand the scope of the issue:

bash
# Check the error message
git status

# Examine the index file
ls -la .git/index

# Check if the file is empty or suspiciously small
# A normal index is typically at least a few KB
file .git/index

If you see output like:
- error: bad index file sha1 signature
- fatal: index file corrupt
- File size is 0 bytes or very small

Then the index is indeed corrupt and needs to be regenerated.

2Back up any uncommitted changes (if possible)

Before removing the index, try to preserve any uncommitted work. The index stores your staged changes, so if you had changes staged, they may be lost. However, your actual working directory files are separate from the index.

bash
# Your working directory files should still be intact
# List modified files based on filesystem state
find . -newer .git/index -type f 2>/dev/null | head -20

# If you have important uncommitted changes, copy them elsewhere
cp -r . ../backup-$(date +%Y%m%d)

Important: Your committed history is safe. Only staged but uncommitted changes might be affected. If you were in the middle of staging specific hunks or had a carefully curated index, that precise staging state will be lost.

3Remove the corrupt index file

Delete the corrupted index file. This is safe because Git can regenerate it from HEAD:

bash
# Remove the corrupt index
rm -f .git/index

# On Windows (Command Prompt):
del .git\index

# On Windows (PowerShell):
Remove-Item .git\index -Force

If you get a permission denied error, ensure no Git processes or IDE integrations are running, then try again.

4Regenerate the index from HEAD

Rebuild the index from the last commit. You have two options depending on whether you want to preserve working directory changes:

Option A: Reset the index only (preserves working directory changes)

bash
# Regenerate index from HEAD, keeping working directory files
git reset

This recreates the index from your last commit but leaves your working directory files untouched. Modified files will show as "modified" in git status.

Option B: Use read-tree (alternative method)

bash
# Read the tree from HEAD into the index
git read-tree HEAD

This does essentially the same thing as git reset but is more explicit about what it's doing.

After either command:

bash
# Verify the index is working
git status

You should now see a normal status output showing any differences between your working directory and HEAD.

5Re-stage your changes

After regenerating the index, any changes you had staged will need to be re-staged:

bash
# See what files differ from HEAD
git status

# Re-stage all changes
git add -A

# Or stage specific files
git add path/to/file.txt

# If you want to review changes before staging
git diff
git add -p  # Interactive staging

If you had specific files staged before the corruption, you'll need to stage them again. The precise staging state (e.g., partial hunks) cannot be recovered.

6Check for underlying filesystem or process issues

If the corruption happened once due to a crash, you're done. But if it keeps recurring, investigate the root cause:

bash
# Check disk health (Linux)
sudo dmesg | grep -i error
sudo smartctl -a /dev/sda  # Replace with your disk

# Check filesystem (unmount first if possible)
sudo fsck -n /dev/sda1

# On macOS
diskutil verifyVolume /

# Check for processes accessing .git
lsof +D .git

Common recurring causes:
- Antivirus software: Add the repository folder to exclusions
- Cloud sync (Dropbox, OneDrive): Don't sync .git directories
- Failing hardware: Check SMART data, run memory tests
- NFS issues: Consider working with local storage

If the problem recurs on NFS or network storage, Git's index locking may not work correctly. Consider cloning to local storage.

7Verify repository integrity

After recovering from index corruption, verify the rest of your repository is healthy:

bash
# Run Git's filesystem check
git fsck --full

# Check for any missing or corrupt objects
git fsck --connectivity-only

If git fsck reports problems like missing objects or corrupt commits, you may need more extensive recovery. In that case:

bash
# Try to fetch missing objects from remote
git fetch --all

# If objects are still missing, you may need to re-clone
git clone <remote-url> ../repo-fresh

For most index corruption cases, git fsck should report no errors after regenerating the index.

Advanced notes

Understanding the Git Index Format:

The index file (.git/index) has a specific binary format:
1. Header: 12 bytes containing signature ("DIRC"), version, and entry count
2. Entries: Variable-length records for each tracked file containing:
- Metadata (ctime, mtime, dev, ino, mode, uid, gid, file size)
- SHA-1 hash of file contents
- Flags and path name
3. Extensions: Optional data for features like cache tree, resolve-undo
4. SHA-1 checksum: Final 20 bytes validating the entire file

When Git reads the index, it computes the SHA-1 of everything except the last 20 bytes and compares it to the stored checksum. A mismatch means corruption.

Prevention Strategies:

1. Use a UPS: Protect against power loss during Git operations
2. Don't force-kill Git: Let operations complete or use Ctrl+C (SIGINT) which allows cleanup
3. Exclude .git from sync and scanning:
- Add .git to antivirus exclusions
- Exclude from Dropbox/OneDrive/Google Drive sync
- Don't use cloud-synced folders for active development
4. Use local storage: NFS and network filesystems don't guarantee the atomicity Git needs

CI/CD Considerations:

Index corruption in CI/CD often stems from:
- Container/VM interruption: Jobs killed mid-operation
- Shared workspaces: Multiple jobs accessing the same repository
- Stale caches: Cached .git directories from previous failed builds

Prevention in CI/CD:

yaml
# GitLab CI example - clean start
before_script:
  - rm -f .git/index.lock .git/index
  - git reset --hard HEAD
  - git clean -fdx
yaml
# GitHub Actions - fresh checkout
- uses: actions/checkout@v4
  with:
    clean: true

Index Version Compatibility:

Git index format has evolved:
- Version 2: Original format
- Version 3: Added extended flags
- Version 4: Path compression

Newer Git can read older formats, but older Git may not read newer formats. The error "bad index file sha1 signature" can occasionally mean version incompatibility rather than corruption.

Check and set index version:

bash
# See current index version (in hex dump)
xxd -l 8 .git/index

# Force a specific version when Git writes
git config index.version 2
git update-index --index-version 2

Related Lock Files:

Index corruption is different from lock file issues. If you see "index.lock exists", that's a different problem (another process running). The errors are:
- index.lock exists = Lock contention, remove .git/index.lock
- index file corrupt = Data corruption, remove and regenerate .git/index

Emergency Recovery Without Remote:

If you have no remote and the repository is partially corrupt:

bash
# Try to salvage commits from reflog
git reflog

# Recover specific commits
git branch recovered-work <commit-sha>

# Or use git-stash if you had stashed changes
git stash list

The packfiles (.git/objects/pack/) contain your actual history. Index corruption doesn't affect them unless there's broader filesystem damage.

Frequently asked questions

What does "error: bad index file sha1 signature fatal: index file corrupt" mean?
The "index file corrupt" or "bad index file sha1 signature" error indicates that Git's staging area has become unreadable. git/index, is a binary file that tracks the state of your working directory and what's staged for the next commit. When Git detects that this file's checksum doesn't match its contents, it refuses to proceed to protect your repository from further damage.
How do I fix "error: bad index file sha1 signature fatal: index file corrupt"?
This error occurs when Git's index file (.git/index) becomes corrupted, typically due to power loss, system crashes, or abrupt process termination during Git operations. The fix involves removing the corrupted index and regenerating it from HEAD.
What causes "error: bad index file sha1 signature fatal: index file corrupt"?
Power loss or system crash during an active Git operation (most common cause) Abrupt process termination (kill -9, sudden reboot, container shutdown) while Git was writing to the index Antivirus or security software scanning and locking .git/index mid-write
Is the "error: bad index file sha1 signature fatal: index file corrupt" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading