Skip to content

Repository corruption and emergency repair quick reference

One-page cheat sheet for diagnosing repository corruption, unlocking stuck metadata, rebuilding index files, and recovering damaged objects.


1. Quick command matrix

Failure scenario Diagnostic / repair command
Full repository health check git fsck --full --strict
Fast health check in CI git fsck --quick
List stale lock files find .git -name "*.lock" -type f
Clear all stale lock files find .git -name "*.lock" -type f -delete
Fix corrupt index file rm -f .git/index && git reset --mixed HEAD
Fix corrupt or blank HEAD echo "ref: refs/heads/master" > .git/HEAD
Find zero-byte loose objects find .git/objects/ -type f -size 0c
Delete zero-byte loose objects find .git/objects/ -type f -size 0c -delete
Verify all packfiles git verify-pack -v .git/objects/pack/*.pack
Rebuild pack index (.idx) git index-pack <packfile>.pack
Recover missing objects from remote git fetch origin '+refs/heads/*:refs/remotes/origin/*'
Salvage objects from broken packfile git unpack-objects < broken-pack.pack

2. Step-by-step index rebuild

When git status fails with fatal: index file corrupt:

# 1. Back up existing index
mv .git/index .git/index.bak

# 2. Reconstruct index from HEAD commit
git reset --mixed HEAD

# 3. Confirm index matches working directory
git status

3. Step-by-step HEAD recovery

When git log or git status fails with fatal: bad default revision 'HEAD':

# 1. Check if HEAD points to a valid symbolic reference
cat .git/HEAD

# 2. Check the most recent commit in HEAD reflog
tail -n 1 .git/logs/HEAD

# 3. If ref file is zeroed, restore commit hash from reflog
LAST_SHA=$(tail -n 1 .git/logs/HEAD | awk '{print $2}')
echo "$LAST_SHA" > .git/refs/heads/master
echo "ref: refs/heads/master" > .git/HEAD

4. Recovering corrupt loose objects

When git fsck reports error: inflate: data stream error:

# 1. Identify the corrupt object path from fsck output
# e.g., error in .git/objects/3a/81b2...

# 2. Check size and delete if zero bytes
ls -la .git/objects/3a/81b2*
rm -f .git/objects/3a/81b2*

# 3. Pull missing object from upstream remote
git fetch origin

5. Rebuilding pack indices

When Git cannot open pack index files:

cd .git/objects/pack/

# Verify each packfile
for p in *.pack; do
  git verify-pack -v "$p" || git index-pack "$p"
done