This error indicates repository corruption where an object file's content doesn't match its SHA-1 hash filename. Recovery typically involves replacing corrupt objects from a backup or recloning the repository.
The "sha1 mismatch" error occurs when Git's `git fsck` command detects that a stored object's content doesn't match the SHA-1 hash used as its filename. Git stores objects (commits, trees, blobs) in the `.git/objects` directory, with filenames derived from the SHA-1 hash of their content. When you see this error, it means the object file has been corrupted - the bytes on disk no longer hash to the expected value. This can happen due to disk failures, filesystem errors, interrupted writes, or bit rot. The error message shows the expected hash and indicates which object directory contains the corrupt file. This is a serious data integrity issue. While Git's content-addressable storage makes corruption detectable, the corrupt object may contain irreplaceable data like a commit, file content, or directory structure. Recovery depends on having access to the same object from another source (a backup, remote, or another clone).
First, run a full repository check to identify all corrupted objects:
git fsck --fullThis command will list all problematic objects. Note down the full SHA-1 hashes of corrupt objects. The output will look like:
error: sha1 mismatch 87859f196ec9266badac7b2b03e3397e398cdb18
error: object corrupt or missing: 87859f196ec9266badac7b2b03e3397e398cdb18Also check for dangling objects and broken links:
git fsck --lost-foundThis will recover any orphaned objects to .git/lost-found/.
Before attempting recovery, understand what data is affected:
# Try to determine the object type (may fail if severely corrupt)
git cat-file -t <sha1>
# If readable, view the object content
git cat-file -p <sha1>If these commands fail, check if any refs point to this object:
# See if it's a commit in any branch
git branch --contains <sha1> 2>/dev/null
# Check the reflog for references
git reflog | grep <sha1_prefix>Knowing whether the corrupt object is a commit, tree, or blob helps determine recovery options.
If you have a remote with the same commits, you can often recover missing objects:
# Fetch all objects from remote
git fetch origin
# Force fetch specific branch if needed
git fetch origin main:refs/remotes/origin/mainAfter fetching, run fsck again:
git fsck --fullIf the remote has the same object, Git will replace the corrupt local version during fetch. This is the cleanest recovery method when available.
If you have another clone of the repository or a backup:
# From another clone, copy the missing object
# Objects are stored at .git/objects/XX/YYYYYY...
# where XX is the first 2 chars of the SHA-1
# Example: for object 87859f196ec9266badac7b2b03e3397e398cdb18
# Copy from backup_repo/.git/objects/87/859f196ec9266badac7b2b03e3397e398cdb18
# to corrupt_repo/.git/objects/87/859f196ec9266badac7b2b03e3397e398cdb18
cp /path/to/backup/.git/objects/87/859f196ec9... .git/objects/87/Alternatively, use git to copy objects:
# Add the backup as a remote and fetch
git remote add backup /path/to/backup/repo
git fetch backupIf the corrupt object is a blob (file content), and you have the original file:
# Recreate the blob object
git hash-object -w /path/to/original/fileThis creates a new blob object with the file's content. The SHA-1 must match what Git expects. If you don't have the exact file content, this won't work.
For files still in your working directory:
# Check if the file exists and matches what Git expects
git status
git diffIf files exist but aren't tracked correctly, you may need to re-add them:
git add <file>
git commit -m "Recover after corruption"If objects cannot be recovered, you may need to start fresh:
# Save any local changes first
git stash
# Or copy uncommitted files to a safe location
cp -r * /path/to/backup/
# Reclone the repository
cd ..
rm -rf corrupt_repo
git clone <remote_url> repo
# Restore any local changes
cp -r /path/to/backup/* repo/If you have local commits not pushed to remote:
# Try to export patches first (may fail for corrupt commits)
git format-patch origin/main
# Apply patches in new clone
git am *.patchAfter recovery, investigate the root cause:
# Check disk health (Linux)
sudo smartctl -a /dev/sda
# Check filesystem for errors
sudo fsck -n /dev/sda1
# Check system logs for disk errors
dmesg | grep -i "error\|fail\|bad"
journalctl -k | grep -i diskFor memory issues:
# Run memory test (requires reboot)
sudo memtest86+Consider enabling Git's built-in transfer integrity checking:
git config --global transfer.fsckObjects true
git config --global receive.fsckObjects true
git config --global fetch.fsckObjects trueThis validates objects during fetch/push/receive operations.
Understanding Git's Object Storage: Git stores objects in .git/objects using content-addressable storage. The SHA-1 hash is computed from the object content plus a header. Loose objects are stored in .git/objects/XX/YYYY... while packed objects are in .git/objects/pack/. SHA-1 mismatches in pack files show additional errors like "index CRC mismatch" or "inflate: data stream error".
Pack File Corruption: If corruption is in a pack file, you may see different errors:
error: SHA1 checksum mismatch for ./objects/pack/pack-xxx.pack
fatal: index file corruptTry repacking or unpacking:
# Unpack all pack files
git unpack-objects < .git/objects/pack/pack-xxx.pack
# Or repack everything
git repack -a -dGit's fsck.skipList: For known corrupt objects you want to ignore (use with caution):
# Create a file with SHA-1s to skip
echo "87859f196ec9266badac7b2b03e3397e398cdb18" > .git/skip-list
git config fsck.skipList .git/skip-listNetwork Filesystem Considerations: NFS and CIFS can cause corruption due to caching and synchronization issues. Consider using core.fsyncObjectFiles:
git config core.fsyncObjectFiles truePreventing Future Corruption:
- Enable object verification: git config transfer.fsckObjects true
- Use reliable storage with ECC memory
- Maintain regular backups and multiple remotes
- Run periodic git fsck as part of maintenance
- Consider using Git's built-in maintenance: git maintenance start
SHA-256 Transition: Git is transitioning from SHA-1 to SHA-256. Future versions will use SHA-256 for object naming, which provides better collision resistance and integrity checking.
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