How to fix error: unable to read sha1 file of file.txt in Git

GitINTERMEDIATEHIGHUpdated Dec 6, 2025ยท 203 views

This error occurs when Git cannot locate or read an object file in its internal database. The fix involves verifying repository integrity, recovering missing objects from remotes or backups, or re-cloning the repository.

Quick fix

Fixes error: unable to read sha1 file of file.txt

git fsck --full
  1. Run git fsck to diagnose the corruption
  2. Try to fetch missing objects from remote
  3. Remove empty or corrupt object files
  4. Recover objects from a backup or another clone
git โ€” error
git fsck --full
error: unable to read sha1 file of file.txt

What this error means

The "unable to read sha1 file" error in Git indicates that the repository's object database is corrupted or incomplete. Git stores all content (files, commits, trees) as objects in the `.git/objects` directory, each identified by a unique SHA-1 hash. When Git references an object that should exist but cannot be found or read, this error appears. Git's object storage works by computing a 40-character SHA-1 hash of each piece of content (with a header indicating type and size). The first two characters of this hash form a subdirectory name under `.git/objects`, and the remaining 38 characters become the filename. For example, an object with hash `b7e23ec29af22b0b4e41da31e868d57226121c84` is stored at `.git/objects/b7/e23ec29af22b0b4e41da31e868d57226121c84`. When this error occurs, it typically means either the object file is missing entirely, the file exists but is corrupted (0 bytes or invalid content), or the object reference in a tree or commit points to a non-existent SHA-1. This can happen after disk failures, interrupted Git operations, filesystem corruption, or issues with partial/shallow clones.

Typical symptoms

  • Git commands fail with 'error: unable to read sha1 file of <filename>'
  • git status or git log shows corruption warnings
  • git fsck reports 'broken link' or 'missing object' errors
  • Checkout operations fail for specific files or branches
  • Repository operations hang or crash unexpectedly

Common causes

  1. Disk failure or filesystem corruption damaged object files
  2. Git operation was interrupted mid-write (system crash, kill signal)
  3. Object file was accidentally deleted from .git/objects directory
  4. Shallow clone or partial clone is missing required objects
  5. Network issues during fetch or clone left incomplete objects
  6. Antivirus or backup software interfered with Git's object files

How to fix "error: unable to read sha1 file of file.txt"

1Run git fsck to diagnose the corruption

First, identify the extent of the corruption by running Git's file system check:

bash
git fsck --full

This will report all corrupted, missing, or dangling objects. Common output includes:
- broken link from tree <sha1> to blob <sha1> - a tree references a missing file
- missing blob <sha1> - a file object is missing
- invalid sha1 pointer - a reference points to nothing

For more detail about which files are affected:

bash
git fsck --full --name-objects
2Try to fetch missing objects from remote

If you have a remote repository (GitHub, GitLab, etc.), Git may be able to recover the missing objects:

bash
git fetch --all

Or fetch with full depth if you have a shallow clone:

bash
git fetch --unshallow

After fetching, run git fsck --full again to check if the issue is resolved.

3Remove empty or corrupt object files

Sometimes Git creates 0-byte object files that block operations. Find and remove them:

bash
# Find empty files in .git/objects (Linux/macOS)
find .git/objects -type f -size 0

# Remove empty files (verify the list first!)
find .git/objects -type f -size 0 -delete

After removing empty files, fetch from remote again:

bash
git fetch origin

This allows Git to re-download the missing objects.

4Recover objects from a backup or another clone

If you have a backup of the repository or another working clone, you can extract the missing objects:

From a packed backup:

bash
# In the backup repository
git cat-file blob <missing-sha1> > recovered-file.dat

# In the damaged repository
git hash-object -w recovered-file.dat

Copy objects directly:

bash
# Copy missing object from backup (adjust path based on SHA-1)
# For sha1 = abc123..., copy from .git/objects/ab/c123...
cp /path/to/backup/.git/objects/ab/c123... .git/objects/ab/
5Reset the index and working tree

If the corruption is in the index (staging area) rather than the objects, reset it:

bash
# Remove the index file
rm .git/index

# Rebuild it from HEAD
git reset HEAD

For cache-tree corruption specifically:

bash
git update-index --refresh
6Clone fresh and migrate changes (cleanest solution)

If the corruption is severe, the safest approach is to clone fresh and copy your work:

bash
# Clone the repository fresh
git clone <remote-url> fresh-repo

# Copy your uncommitted changes (excluding .git)
rsync -av --exclude='.git' ./damaged-repo/ ./fresh-repo/

# Or manually copy changed files
cp -r src/ fresh-repo/src/

Then use the fresh repository going forward and verify with git status.

7Use git-repair for automated recovery

The git-repair tool can automatically fix many types of repository corruption:

Install git-repair:

bash
# Debian/Ubuntu
sudo apt install git-repair

# macOS with Homebrew
brew install git-repair

Run the repair:

bash
git-repair

This tool will attempt to fetch missing objects from remotes and repair references automatically.

8Verify the repository is healthy

After recovery, confirm the repository is fully functional:

bash
# Full integrity check
git fsck --full

# Test common operations
git status
git log --oneline -10
git branch -a

If git fsck reports no errors, the repository has been successfully repaired.

Advanced notes

Understanding Git's object storage: Git stores four types of objects: blobs (file contents), trees (directory listings), commits, and tags. Each object is compressed with zlib and named by its SHA-1 hash. When Git cannot read an object, it means the file at .git/objects/XX/XXXXXXXX... is missing, empty, or corrupted.

Packed objects: Over time, Git packs loose objects into .git/objects/pack/*.pack files for efficiency. If corruption is in a pack file, you may need to repack after recovery:

bash
git repack -a -d

Partial and shallow clones: These intentionally omit objects to save space. If you encounter this error with a shallow clone, converting to a full clone usually resolves it:

bash
git fetch --unshallow

Preventing future corruption:
- Avoid interrupting Git operations (especially during gc, repack, or fetch)
- Use a reliable filesystem with journaling (ext4, APFS, NTFS)
- Keep backups of important repositories
- Run git fsck periodically to catch issues early
- If using network drives, ensure stable connections during Git operations

CI/CD cached clones: In CI environments, cached Git directories can become corrupted over time. If this error appears in CI, clearing the cache or using a fresh clone typically resolves it.

Frequently asked questions

What does "error: unable to read sha1 file of file.txt" mean?
The "unable to read sha1 file" error in Git indicates that the repository's object database is corrupted or incomplete. git/objects directory, each identified by a unique SHA-1 hash. When Git references an object that should exist but cannot be found or read, this error appears.
How do I fix "error: unable to read sha1 file of file.txt"?
This error occurs when Git cannot locate or read an object file in its internal database. The fix involves verifying repository integrity, recovering missing objects from remotes or backups, or re-cloning the repository.
What causes "error: unable to read sha1 file of file.txt"?
Disk failure or filesystem corruption damaged object files Git operation was interrupted mid-write (system crash, kill signal) Object file was accidentally deleted from .git/objects directory
Is the "error: unable to read sha1 file of file.txt" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading