Git fsck checks repository integrity and reports problems. Understanding its output helps you decide whether errors are harmless or require action.
The Error
Running repository check:
git fsckYou see various error messages:
error: object file .git/objects/4a/2b3c4d... is empty
error: object 4a2b3c4d...: invalid object type
missing blob abc123456...
missing commit def123456...
dangling blob 1a2b3c4d...
dangling commit 2a3b4c5d...
broken link from tree 3a4b5c6d... to blob 4a5b6c7d...
warning: reflog entry references missing objectUnderstanding Fsck Output Types
| Error Type | Severity | Meaning |
|---|---|---|
dangling blob/commit/tree | Low | Orphaned object, not referenced |
unreachable object | Low | Object not reachable from any ref |
missing blob/commit/tree | High | Object expected but not found |
error: object file ... is empty | High | Corrupt or incomplete object |
broken link | High | Reference points to missing object |
duplicate entry | Medium | Same object referenced twice in tree |
error in tree | Medium | Invalid tree structure |
Diagnosis Steps
Run full check:
``bash
git fsck --full --unreachable --dangling
Run with progress:
``bash
git fsck --full --progress
Check specific error types: ```bash # Only missing objects git fsck --full 2>&1 | grep "missing"
# Only dangling objects git fsck --full 2>&1 | grep "dangling"
# Only corrupt objects git fsck --full 2>&1 | grep "error:" ```
Count errors:
``bash
git fsck --full 2>&1 | grep -c "error:"
git fsck --full 2>&1 | grep -c "missing"
git fsck --full 2>&1 | grep -c "dangling"
Solution 1: Handle Dangling Objects (Usually Harmless)
Dangling objects are orphaned but don't affect repository function:
dangling blob 1a2b3c4d5e6f7890
dangling commit 2a3b4c5d6e7f8901View dangling content: ```bash # View dangling blob git cat-file -p 1a2b3c4d5e6f7890
# View dangling commit git show 2a3b4c5d6e7f8901 ```
Recover useful dangling commits:
``bash
git branch recovered-work 2a3b4c5d6e7f8901
Clean up dangling objects:
``bash
git prune
git gc
Or aggressive cleanup:
``bash
git reflog expire --expire=now --all
git gc --prune=now
Solution 2: Fix Empty Object Files
Empty object files indicate corruption:
error: object file .git/objects/4a/2b3c4d5e6f7890 is emptyRemove empty files:
``bash
find .git/objects -type f -empty
Delete them:
``bash
find .git/objects -type f -empty -delete
Fetch replacements from remote:
``bash
git fetch origin --all
Verify repair:
``bash
git fsck --full
Solution 3: Recover Missing Objects
Missing objects break repository operations:
missing blob abc1234567890...
missing commit def1234567890...Identify what's missing:
``bash
git fsck --full 2>&1 | grep "missing" | tee missing.txt
Fetch from remote:
``bash
git fetch origin --all --tags
If still missing, check other clones:
``bash
# From another clone of same repo
rsync -av /path/to/other-clone/.git/objects/ .git/objects/
Check reflog for commit recovery:
``bash
git reflog
# Find the commit hash and recover
git branch recovered def1234567890
Solution 4: Fix Broken Links
Broken links in trees indicate missing child objects:
broken link from tree 3a4b5c6d... to blob 4a5b6c7d...View the broken tree:
``bash
git cat-file -p 3a4b5c6d
Shows entries like:
``
100644 blob 4a5b6c7d... filename.txt
The blob 4a5b6c7d is missing.
Recreate missing blob if you know content:
``bash
echo "correct content" | git hash-object -w --stdin
Or fetch from remote:
``bash
git fetch origin
git checkout HEAD -- filename.txt
Solution 5: Fix Duplicate Entries
Duplicate entries in trees indicate corruption:
error in tree 5a6b7c8d: duplicateEntriesView the tree:
``bash
git cat-file -p 5a6b7c8d
Find commits containing the tree:
``bash
git log --all --pretty=format:'%H %T' | grep 5a6b7c8d
Recreate tree manually:
``bash
git checkout <commit-with-tree>
git rm --cached duplicate-file
git add duplicate-file
git write-tree
Solution 6: Fix Reflog Issues
Reflog may reference missing objects:
warning: reflog entry 'abc1234' references missing objectClear invalid reflog entries:
``bash
git reflog expire --expire=now --all
Or expire only unreachable:
``bash
git reflog expire --expire-unreachable=now --all
Solution 7: Repair Corrupt Pack Files
Pack corruption causes various fsck errors:
error: verify_pack failed
error: packfile .git/objects/pack/*.pack cannot be accessedVerify pack integrity:
``bash
git verify-pack -v .git/objects/pack/*.idx
Remove corrupt pack and re-fetch:
``bash
rm .git/objects/pack/*.pack
rm .git/objects/pack/*.idx
git fetch --all
git repack -a -d
Solution 8: Full Repository Repair
For extensive corruption:
```bash # Step 1: Backup cp -r .git .git-backup
# Step 2: Remove corrupt files find .git/objects -type f -empty -delete
# Step 3: Fetch all from remote git fetch origin --all --tags
# Step 4: Repack git repack -a -d
# Step 5: Prune and gc git prune git gc
# Step 6: Verify git fsck --full ```
Solution 9: Reclone When Unrecoverable
If corruption is too extensive:
```bash # Note current branch and state git branch --show-current git stash list
# Clone fresh cd .. git clone <remote-url> repo-new cd repo-new
# Recreate branches manually if needed git checkout -b feature-branch origin/feature-branch ```
Verification
Run fsck after fixes:
``bash
git fsck --full
Should show only dangling objects (if any) and no errors.
Test basic operations:
``bash
git log --oneline -10
git status
git diff HEAD~1
Verify all branches work:
``bash
git branch -a
for b in $(git branch); do
git checkout "$b" && git log --oneline -1
done
Fsck Options Reference
```bash # Basic check git fsck
# Full check including pack files git fsck --full
# Show unreachable objects git fsck --unreachable
# Show dangling objects git fsck --dangling
# Show connectivity only git fsck --connectivity-only
# Check heads and tags git fsck --heads --tags
# Don't check pack files git fsck --no-full
# Check reflog entries git fsck --reflogs
# With progress indicator git fsck --progress ```
Prevention Strategies
Regular maintenance: ```bash # Weekly git gc
# Monthly git gc --aggressive
# After large operations git prune ```
Monitor with fsck:
``bash
# Add to maintenance script
git fsck --full --quiet || echo "Repository has issues!"
Keep backups:
``bash
git clone --mirror /path/to/repo /backup/repo.git
Avoid interrupting Git: Let operations complete. Don't force-kill Git processes.
Interpreting Fsck Results Summary
| Output | Action Required |
|---|---|
dangling blob | Usually safe to ignore or prune |
dangling commit | Recover if useful, otherwise prune |
unreachable | Safe, will be cleaned by gc |
missing | Fetch from remote, critical |
error: object file empty | Delete and fetch, critical |
broken link | Fix missing object, critical |
duplicate entry | Recreate tree, medium priority |
warning: reflog | Clear reflog, low priority |