This error indicates Git object file corruption, typically caused by interrupted operations, system crashes, or filesystem issues. The fix involves removing empty objects and recovering from a remote repository.
Git stores all repository data (commits, trees, blobs) as objects in the `.git/objects` directory. Each object is identified by its SHA-1 hash and stored in a subdirectory based on the first two characters of that hash. When Git reports an "object file is empty" error, it means one or more of these critical files has zero bytes—the data that should be there is completely missing. This corruption typically occurs when a write operation to the `.git/objects` directory is interrupted before completion. Git normally writes objects to a temporary file first, then renames it atomically, but certain conditions can still leave empty files behind. Common culprits include: - Your laptop running out of battery during a Git operation (commit, fetch, merge) - A virtual machine or container being forcefully stopped - File system bugs, especially with network filesystems (NFS, VirtualBox shared folders, CIFS) - Hard drive failures or filesystem corruption - The system running out of disk space mid-write The good news is that if you have a remote repository (GitHub, GitLab, etc.), you can usually recover completely by fetching the missing objects. If you only have a local repository with no remote, recovery may be partial or impossible depending on which objects were corrupted.
Before attempting any repairs, create a backup of your entire .git directory. If something goes wrong during recovery, you'll need this.
cp -a .git .git-backupCreate additional backups after each major step using different names (.git-backup-1, .git-backup-2, etc.).
Use Git's built-in filesystem check to get a complete list of problems:
git fsck --fullThis will report all issues including empty files, corrupt objects, and missing references. Note down any error messages—you may see multiple corrupt objects.
Example output:
error: object file .git/objects/ab/cd1234... is empty
error: object file .git/objects/ef/gh5678... is empty
missing blob abc123...Delete all zero-byte files in the objects directory. Git cannot use these files anyway, and removing them allows Git to fetch fresh copies from the remote.
find .git/objects/ -type f -empty -deleteThis command finds all empty files recursively in .git/objects/ and removes them.
Alternative: Remove a specific corrupt object manually:
rm .git/objects/ab/cd1234...Replace ab/cd1234... with the path shown in the error message.
If you have a remote repository configured, fetch to download the missing objects:
git fetch originThis retrieves all objects from the remote that aren't in your local repository, including replacements for the ones you just deleted.
If you have multiple remotes or need objects from a specific branch:
git fetch --allRun git fsck again to confirm all issues are resolved:
git fsck --fullAt this point, there should be no errors (warnings about dangling objects are usually fine). Test basic operations:
git status
git log --oneline -5If these work without errors, your repository is recovered.
If you're still seeing errors about HEAD or the index after fixing object files, you may need to reset them:
# If HEAD is corrupt
git symbolic-ref HEAD refs/heads/main
# Use 'master' instead of 'main' if that's your default branch
# If the index is corrupt
rm .git/index
git resetThis rebuilds the index from the current HEAD commit.
After recovery, clean up any unreferenced objects:
git pruneThen run a garbage collection to optimize the repository:
git gcThis packs loose objects and removes unreachable ones, which can prevent future issues.
If the above steps don't work, you can replace the entire .git directory from a fresh clone while keeping your working directory changes:
# Save your current work
git stash --include-untracked 2>/dev/null || true
# Clone fresh in a different location
git clone <remote-url> ../repo-fresh
# Replace .git directory
rm -rf .git
mv ../repo-fresh/.git .
# Verify and check status
git statusThis gives you a clean repository state. Any uncommitted changes in your working directory will appear as modifications that you can review and commit.
### Why This Happens
Git objects are the fundamental storage units—blobs contain file content, trees represent directories, and commits point to trees with metadata. When any of these becomes empty or corrupt, Git cannot reconstruct the repository state.
Git is designed to be resilient: it writes to temporary files then renames them atomically. However, this protection can fail when:
1. Filesystem bugs: VirtualBox shared folders (vboxsf) and some network filesystems don't guarantee atomic renames
2. Disk caching: If the OS caches writes and power fails before flushing, data is lost
3. Insufficient permissions: If Git can't complete a rename, the temporary file may be left behind
### Prevention
Consider enabling core.fsyncobjectfiles for critical repositories:
git config core.fsyncobjectfiles trueThis forces Git to fsync each object file, ensuring it's written to disk before continuing. This is slower but safer.
### Using git-repair Tool
For complex corruption, the git-repair tool can automate many recovery steps:
# Install (Debian/Ubuntu)
sudo apt install git-repair
# Run repair
git-repairThis tool attempts various recovery strategies automatically.
### When Recovery Fails
If you don't have a remote and critical objects are corrupted:
1. Check for backups (Time Machine, system backups)
2. Use git unpack-objects with a packfile from any working clone
3. As a last resort, git init a new repository and copy your working tree—you'll lose history but keep current files
### Reflog as Recovery Source
Sometimes git reflog contains references to commits that can help recovery:
git reflogIf you see the commit you need, try checking it out directly by SHA.
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