Blog
6 min read

Git "error: object file is empty": How to Recover a Corrupted Repository

Git error object file is empty or loose object is corrupt after a crash or full disk? Back up .git, delete empty objects, re-fetch, and check with git fsck.

error: object file .git/objects/xx/... is empty means one or more files in Git's object database were created but never written, almost always because the machine crashed, lost power, or ran out of disk space while Git was working. If the repository has a remote, the usual fix is: back up .git, delete the empty object files, run git fetch to download good copies, and confirm with git fsck. If that doesn't work, re-clone and copy your working files across.

What the error looks like

You might see any of these, often several together:

error: object file .git/objects/bb/2e5a534230bb9c8a0dec98d6a8d669a9d15e2a is empty
error: object file .git/objects/bb/2e5a534230bb9c8a0dec98d6a8d669a9d15e2a is empty
fatal: bad object HEAD
fatal: loose object bb2e5a534230bb9c8a0dec98d6a8d669a9d15e2a (stored in .git/objects/bb/2e5a534230bb9c8a0dec98d6a8d669a9d15e2a) is corrupt
error: bad tree object HEAD

Every commit, folder listing (tree) and file version (blob) Git stores lives as an "object" under .git/objects/. A "loose" object is one stored as its own small compressed file. If one of those files is zero bytes, Git can't read it, and anything that depends on it (the current commit, a folder, a file's history) breaks.

Why it happens

  • The computer crashed or lost power in the middle of a commit, checkout or fetch. Git had created the file but the operating system never flushed the contents to disk.
  • The disk filled up. Git can create a file but not write into it. If you also see No space left on device anywhere, start with freeing up disk space or the recovery will just fail again.
  • A sync tool touched .git. Dropbox, OneDrive, iCloud Drive and similar tools sometimes produce half-synced or conflicting copies of files inside .git. Keeping repositories outside synced folders avoids this.
  • A virtual machine or container was killed while Git was writing to a mounted volume.

Your working files (the actual code you see in your editor) are separate from .git/objects. A corrupted object store does not mean your current, uncommitted edits are gone.

Step 0: back up before you touch anything

Recovery involves deleting files inside .git. Make a full copy first so any mistake is reversible:

cd ..
cp -a my-project my-project-backup
cd my-project

This copies your working files and .git together. On Windows, copying the folder in Explorer works too (make sure hidden files are included). If the disk is full, free space before copying.

Also stop anything that might be writing to the repository: close your IDE, Git GUIs, and any file-sync app watching the folder.

Step 1: find the empty object files

find .git/objects/ -type f -empty

This lists every zero-byte file in the object store. There may be one or dozens, depending on how much Git was doing when it was interrupted.

Step 2: delete them

find .git/objects/ -type f -empty -delete

An empty object file contains no data, so deleting it loses nothing. It turns an "unreadable" object into a "missing" one, which Git knows how to fetch again.

On Windows without a Unix shell, run the same commands in Git Bash, which ships with Git for Windows.

Step 3: fetch good copies from the remote

git fetch origin

If the missing objects exist on the remote (they will, for anything you'd already pushed), this often brings them back. Then check:

git fsck --full

git fsck "verifies the connectivity and validity of the objects in the database", according to the git-fsck docs. No output, or only lines starting with dangling, means the repository is healthy. Dangling objects are harmless leftovers.

If git fsck still reports missing blob, missing tree or broken link, ask Git to download everything again instead of negotiating with the server about what you already have:

git fetch --refetch origin
git fsck --full

The git-fetch docs describe --refetch as fetching "all objects as a fresh clone would". On an old Git version without this flag, skip to the re-clone fallback below.

Step 4: check your branch and working tree

git status
git log --oneline -5

If git status complains about the index (for example index file corrupt or invalid sha1 pointer in cache-tree), the staging area file was damaged too. Rebuild it from your current commit:

rm .git/index
git reset

git reset without --hard rebuilds the index and leaves your working files alone. Afterwards, git status will show your real uncommitted changes again.

What if the corrupted commits were never pushed?

Fetching can only restore objects the remote has. If you committed locally and the crash hit before you pushed, those commits' objects may be truly gone. A few things to try:

  • git fsck --full names the missing objects. If only a blob (one file version) is missing and you still have that file in your working tree, recommitting the file recreates it.
  • Your backup copy, another clone on a different machine, or a teammate's clone may still have the objects.
  • git reflog shows where your branch pointed recently. If the latest commit is broken but the one before it is intact, you can move the branch back with git reset --soft <good-commit> and recommit your working files on top. See how to undo almost anything in Git for how reflog and reset work.

Fallback: re-clone and copy your work across

If the steps above don't produce a clean git fsck, start from a fresh clone. This is slower but very reliable:

cd ..
git clone <your-remote-url> my-project-fresh

Then copy your working files (not the .git folder) from the broken copy into the fresh one, and check what changed:

rsync -a --exclude .git my-project/ my-project-fresh/
cd my-project-fresh
git status
git diff

rsync with --exclude .git brings over your edits without the corrupted object store. git diff then shows exactly what you hadn't committed yet. Commit, push, and once you're satisfied, delete the broken copy. Our git clone guide covers cloning options if your remote needs SSH or a token.

Preventing it next time

  • Push often. Anything on the remote can be fetched back. Local-only commits are the ones you can lose.
  • Keep repositories out of synced folders (Dropbox, OneDrive, iCloud Drive).
  • Watch free disk space, especially on small laptops and VMs.
  • Keep backups of anything that isn't pushed. The backups for beginners guide covers a simple setup.

Summary

  1. Back up the whole project folder.
  2. find .git/objects/ -type f -empty -delete
  3. git fetch origin, then git fsck --full.
  4. Still broken: git fetch --refetch origin and git fsck --full again.
  5. Rebuild a damaged index with rm .git/index && git reset.
  6. Last resort: fresh clone, copy working files across excluding .git.

EasySpawn keeps your workspace on a persistent cloud server, so a laptop crash or full laptop disk doesn't take your repository's working copy with it. See how it works or join the waitlist.

Related: "No Space Left on Device": Finding and Freeing Disk Space on a Server · How to Undo Almost Anything in Git (Including an AI Agent's Mess) · git clone Explained: Copy a Repository to Your Computer · Backups for Beginners: How to Not Lose Your App's Data

Keep reading