The 'unable to unpack header' error indicates corruption in a Git object file. This typically results from disk corruption, interrupted operations, or file system issues affecting the .git/objects directory.
The "unable to unpack header" error in Git occurs when Git attempts to read an object from its internal object database and fails to decompress or parse the object's header. Every Git object (commit, tree, blob, or tag) is stored as a compressed file in the `.git/objects` directory, and each object begins with a header that identifies its type and size. When you see this error, it means the object file itself exists but is corrupted - either the compression data is invalid, the header bytes are malformed, or the file contains garbage data. This is different from a "missing object" error, which would indicate the file doesn't exist at all. The corruption is almost always caused by external factors rather than Git itself. Common culprits include power outages during write operations, failing hard drives, overeager antivirus software, file system bugs, or memory corruption. In rare cases, the corruption may have been introduced during a network transfer if you pushed or pulled over an unreliable connection.
Before attempting any repair, create a complete backup of your repository:
# Create a tarball backup
tar -czvf repo-backup.tar.gz /path/to/your/repo
# Or use rsync for a copy
rsync -av /path/to/your/repo/ /path/to/backup/repo-copy/This ensures you can recover if something goes wrong during the repair process.
Use Git's file system check to find all corrupt objects:
# Full repository check
git fsck --full
# For more details about corrupt objects
git fsck --full --name-objectsThis will list all corrupt or missing objects with their hashes. Note down the object hashes for the next steps. You may see output like:
error: unable to unpack abc1234... header
error: inflateEnd: data stream error (unknown compression method)
missing blob abc1234...Corrupt loose objects can be safely removed - Git stores object hashes as files where the first two characters are the directory name:
# For an object hash like abc1234..., the file is at:
# .git/objects/ab/c1234...
# Remove the corrupt object file
rm .git/objects/ab/c1234*
# Run fsck again to see remaining issues
git fsck --fullRepeat this for each corrupt object identified by git fsck.
If corruption is in a pack file, try to recover what you can:
# Move the corrupt pack file aside
mv .git/objects/pack/pack-*.pack ./pack-backup.pack
mv .git/objects/pack/pack-*.idx ./pack-backup.idx
# Try to unpack recoverable objects
git unpack-objects -r < pack-backup.packThe -r flag tells Git to recover as much as possible even if some objects are corrupt.
If you have a remote repository, fetch missing objects:
# First, remove the index to force rebuild
rm .git/index
# Fetch all objects from remote
git fetch --all --refetch
# Or try a more aggressive fetch
git fetch origin --prune
# Reset to the remote branch state
git reset --hard origin/mainThis will download fresh copies of any objects available on the remote.
If you have a clean copy elsewhere, you can recover objects from it:
# Clone a fresh copy
git clone <remote-url> fresh-clone
# Unpack objects from the fresh clone into your repo
cd /path/to/corrupt/repo
git unpack-objects < ../fresh-clone/.git/objects/pack/pack-*.packThis imports all objects from the fresh clone into your repository.
After recovery, verify the repository is healthy:
# Run fsck again to verify
git fsck --full
# Clean up unreachable objects
git reflog expire --expire-unreachable=now --all
git gc --prune=now
# Test basic operations
git log --oneline -10
git statusIf git fsck completes without errors, your repository is recovered.
If recovery fails, your best option is to re-clone the repository:
# Save any uncommitted changes
cp -r /path/to/corrupt/repo/src /tmp/saved-changes
# Remove the corrupt repository
rm -rf /path/to/corrupt/repo
# Clone fresh from remote
git clone <remote-url> /path/to/repo
# Restore any saved changes
cp -r /tmp/saved-changes/* /path/to/repo/src/This gives you a clean repository. You'll lose any unpushed commits, but gain a fully functional repository.
### Understanding Git Object Storage
Git stores all content as objects in the .git/objects directory. Each object is compressed using zlib and named by its SHA-1 hash. The object header contains the type (blob, tree, commit, tag) and size, followed by a null byte and the actual content.
The "unable to unpack header" error specifically means Git couldn't decompress the first few bytes of the object file, which should contain this header information.
### Loose Objects vs Pack Files
Git stores objects in two formats:
- Loose objects: Individual compressed files in .git/objects/XX/XXXXXXXX...
- Pack files: Multiple objects combined into .git/objects/pack/pack-*.pack
Corruption in pack files is more serious because one pack file may contain thousands of objects. Use git unpack-objects -r to salvage what you can.
### Diagnosing Hardware Issues
If you experience repeated corruption:
# Check disk health (Linux)
sudo smartctl -a /dev/sda
# Check filesystem for errors
sudo fsck -n /dev/sda1
# Test memory (run memtest86 from boot media)Consider moving your repository to a different disk or running memory tests if corruption recurs.
### Preventing Future Corruption
1. Enable core.fsync: Git 2.36+ has improved fsync options
git config --global core.fsyncMethod batch2. Use UPS: A battery backup prevents power-loss corruption
3. Exclude .git from antivirus: Some antivirus software corrupts Git objects
4. Regular backups: Push to multiple remotes or use git bundle
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