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.
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.
First, confirm that the index is actually corrupt and understand the scope of the issue:
# 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/indexIf 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.
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.
# 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.
Delete the corrupted index file. This is safe because Git can regenerate it from HEAD:
# Remove the corrupt index
rm -f .git/index
# On Windows (Command Prompt):
del .git\index
# On Windows (PowerShell):
Remove-Item .git\index -ForceIf you get a permission denied error, ensure no Git processes or IDE integrations are running, then try again.
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)
# Regenerate index from HEAD, keeping working directory files
git resetThis 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)
# Read the tree from HEAD into the index
git read-tree HEADThis does essentially the same thing as git reset but is more explicit about what it's doing.
After either command:
# Verify the index is working
git statusYou should now see a normal status output showing any differences between your working directory and HEAD.
After regenerating the index, any changes you had staged will need to be re-staged:
# 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 stagingIf 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.
If the corruption happened once due to a crash, you're done. But if it keeps recurring, investigate the root cause:
# 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 .gitCommon 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.
After recovering from index corruption, verify the rest of your repository is healthy:
# Run Git's filesystem check
git fsck --full
# Check for any missing or corrupt objects
git fsck --connectivity-onlyIf git fsck reports problems like missing objects or corrupt commits, you may need more extensive recovery. In that case:
# 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-freshFor most index corruption cases, git fsck should report no errors after regenerating the index.
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:
# GitLab CI example - clean start
before_script:
- rm -f .git/index.lock .git/index
- git reset --hard HEAD
- git clean -fdx# GitHub Actions - fresh checkout
- uses: actions/checkout@v4
with:
clean: trueIndex 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:
# 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 2Related 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:
# 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 listThe packfiles (.git/objects/pack/) contain your actual history. Index corruption doesn't affect them unless there's broader filesystem damage.
fatal: bad object in rev-list input
Git rev-list encounters bad or invalid object
fatal: Out of memory, malloc failed during pack operation
Out of memory during Git pack operation
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git