The 'fatal: bad object HEAD' error indicates your Git repository's HEAD reference is corrupted or points to a missing commit object. This typically results from filesystem corruption, interrupted operations, or cloud sync conflicts. Recovery usually involves fetching from remote or restoring HEAD from backup.
The "fatal: bad object HEAD" error means Git cannot find or read the commit object that HEAD is supposed to reference. In Git, HEAD is a special reference that points to the current commit (usually via a branch reference like refs/heads/main). When Git tries to resolve HEAD but finds that the underlying commit object is missing, corrupted, or unreadable, it reports this fatal error. Git stores all repository data as objects (commits, trees, blobs) in the .git/objects directory, with each object identified by a SHA-1 hash. HEAD typically contains either a symbolic reference (like "ref: refs/heads/main") or a direct commit hash. If the commit hash HEAD points to doesn't exist in the object database, or if the file is corrupted, Git cannot determine the repository state and refuses to operate. This error is particularly alarming because it affects the core repository reference. Without a valid HEAD, most Git operations fail immediately. However, the error usually doesn't mean your files or commit history are lost—just that Git's internal pointers need repair.
First, check the extent of repository corruption:
# Check repository integrity
git fsck --full
# For more verbose output
git fsck --full --verbose
# Check for unreachable objects that might help recovery
git fsck --unreachable --no-reflogsThis will report missing objects, dangling commits, and other integrity issues. Note the error messages—they'll guide your recovery approach.
If you have a remote repository, fetching often resolves the issue by downloading missing objects:
# Fetch all branches from origin
git fetch origin
# More thorough fetch that re-downloads pack files
git fetch --refetch
# If the above fails, try fetching a specific branch
git fetch origin mainAfter fetching, try git status again. This fix works in most cases where the corruption was caused by sync issues or interrupted operations.
If fetching doesn't work, try resetting HEAD manually using the reflog:
# View recent HEAD positions from the reflog
cat .git/logs/HEAD
# Find the last valid commit hash (40-character SHA)
# Then reset HEAD to that commit
git reset --hard <commit-hash>
# Alternative: view reflog if it's accessible
git reflogThe reflog at .git/logs/HEAD contains a history of HEAD positions. Look for a recent valid commit hash and use it to reset.
If a specific branch reference is corrupted, repair it manually:
# Check what HEAD points to
cat .git/HEAD
# If it shows "ref: refs/heads/main", check that reference
cat .git/refs/heads/main
# If the branch file is empty or corrupted, replace it with a valid commit
# Find a valid commit from logs or remote
git ls-remote origin main
# Update the branch reference with the correct hash
echo "abc123def456..." > .git/refs/heads/mainReplace abc123def456... with the actual full 40-character commit hash from your remote.
If the issue is with remote tracking branches (common after branch renames):
# Auto-detect and set the remote HEAD
git remote set-head origin --auto
# Clean up stale remote references
git remote prune origin
# Run garbage collection to clean up
git gcThis is especially useful when a repository's default branch was renamed (e.g., master to main) and local references became stale.
If the .git directory is severely corrupted but you have a remote:
# Backup your current .git folder first
mv .git .git.backup
# Reinitialize the repository
git init
# Add your remote back
git remote add origin <your-remote-url>
# Fetch all data from remote
git fetch origin
# Checkout your branch
git checkout mainThis preserves your working directory files while replacing corrupted Git metadata. Check for any uncommitted changes in .git.backup that may need recovery.
As a last resort, clone a fresh copy and migrate your changes:
# Clone repository to a new location
git clone <your-remote-url> repo-fresh
# Copy your current working files (excluding .git)
# From your corrupted repo directory:
rsync -av --exclude='.git' ./ ../repo-fresh/
# Or on Windows:
xcopy /E /I /EXCLUDE:.git . ..\repo-fresh
# Switch to fresh clone and check status
cd ../repo-fresh
git statusThis ensures you have a clean repository state while preserving any local file changes.
The git-repair tool can automate many recovery steps:
# Install git-repair (Debian/Ubuntu)
sudo apt install git-repair
# Install on macOS
brew install git-repair
# Run automated repair
git-repair
# Force repair even if some data may be lost
git-repair --forcegit-repair runs fsck and attempts various automated fixes. It's particularly useful for complex corruption scenarios.
### Understanding Git Objects and HEAD
Git's object database stores four types of objects:
- Blobs: File contents
- Trees: Directory structures
- Commits: Snapshots with metadata and parent references
- Tags: Named references to specific objects
HEAD is stored in .git/HEAD and typically contains a symbolic reference like ref: refs/heads/main. The branch file (.git/refs/heads/main) then contains the 40-character SHA-1 hash of the current commit.
When any link in this chain is broken, you get "bad object HEAD".
### Preventing Future Corruption
Avoid syncing .git folders with cloud services:
# Add .git to cloud sync exclusions
# Dropbox: Create .dropboxignore file
echo ".git" >> .dropboxignore
# Or use Git-specific sync solutions like:
# - GitHub/GitLab hosting
# - Git LFS for large filesEnable automatic garbage collection limits:
# Set loose object auto-gc threshold
git config --global gc.auto 256
# Enable auto-packing to reduce loose objects
git config --global gc.autoPackLimit 50### Recovering Uncommitted Changes
If you had uncommitted changes before corruption, check:
# Look for cached changes in the index
git ls-files -s
# Check stash for saved work
git stash list
# Look in lost-found after fsck
git fsck --lost-found
ls .git/lost-found/### WSL and Windows-Specific Issues
When using Git in WSL with repositories on Windows drives:
# Ensure proper line ending handling
git config --global core.autocrlf input
# Avoid filesystem-intensive operations on /mnt/c
# Instead, keep repos in the Linux filesystem (~/)Windows NTFS and WSL2 can have permission and sync issues that lead to corruption. Keep Git repositories in the native Linux filesystem when possible.
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
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git