fsck Git Command Guide
The git fsck command is used to verify the connectivity and validity of the objects in the Git database. It checks the integrity of the repository and can help identify and recover from repository corruption or orphaned objects.
git fsck Syntax:
Section titled “git fsck Syntax:”git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs] [--[no-]full] [--strict] [--verbose] [--lost-found] [--[no-]dangling] [--[no-]progress] [--connectivity-only] [--[no-]name-objects] [--[no-]references] [<object>...]Options:
Section titled “Options:”| Option | Description |
|---|---|
| —tags | Report tags |
| —root | Report root nodes |
| —unreachable | Print unreachable objects |
| —[no-]dangling | Print dangling objects (default: dangling) |
| —cache | Consider index entries as head nodes |
| —no-reflogs | Don’t consider reflogs for reachability |
| —[no-]full | Check alternate object pools (default: full) |
| —connectivity-only | Check only connectivity, skip blob contents |
| —strict | Enable strict checking |
| —verbose | Provide detailed output |
| —lost-found | Write dangling objects to .git/lost-found |
| —name-objects | Show object names with SHAs |
| —[no-]progress | Control progress reporting |
| —[no-]references | Check references database consistency |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
| [ | Specific objects to treat as trace heads |
git fsck Command Samples:
Section titled “git fsck Command Samples:”Basic repository integrity check
Section titled “Basic repository integrity check”git fsckCheck the entire repository for connectivity and validity issues.
Show unreachable objects only
Section titled “Show unreachable objects only”git fsck --unreachableFind objects that exist but aren’t reachable from any reference.
Fast connectivity check
Section titled “Fast connectivity check”git fsck --connectivity-onlyCheck only connectivity of reachable objects, skip blob content verification.
Verbose checking with names
Section titled “Verbose checking with names”git fsck --verbose --name-objectsProvide detailed output with descriptive object names.
Check with strict rules
Section titled “Check with strict rules”git fsck --strictEnable strict checking to catch issues from older Git versions.
Write dangling objects to files
Section titled “Write dangling objects to files”git fsck --lost-foundWrite dangling objects to .git/lost-found/ for potential recovery.
Check without reflog consideration
Section titled “Check without reflog consideration”git fsck --no-reflogsDon’t consider reflog entries when determining reachability.
Full check including alternates
Section titled “Full check including alternates”git fsck --fullCheck not just main object directory but also alternate object pools.
Check from specific commit
Section titled “Check from specific commit”git fsck HEAD~10Check integrity starting from a specific commit as head.
Check without progress display
Section titled “Check without progress display”git fsck --no-progressRun fsck without progress output.
How do I check repository integrity?
Section titled “How do I check repository integrity?”To check repository integrity, run:
git fsckHow can I find unreachable objects?
Section titled “How can I find unreachable objects?”To find unreachable objects, use —unreachable:
git fsck --unreachableHow do I speed up fsck checks?
Section titled “How do I speed up fsck checks?”To speed up checks, use —connectivity-only to skip blob verification:
git fsck --connectivity-onlyHow can I recover dangling objects?
Section titled “How can I recover dangling objects?”To recover dangling objects, use —lost-found to write them to files:
git fsck --lost-foundWhat’s the difference between unreachable and dangling?
Section titled “What’s the difference between unreachable and dangling?”Unreachable objects exist but aren’t reachable from refs or reflogs. Dangling objects exist in packs but aren’t directly referenced by other objects.
How do I check only reachable objects?
Section titled “How do I check only reachable objects?”To check only reachable objects, use —connectivity-only —no-dangling:
git fsck --connectivity-only --no-danglingHow does strict checking differ from normal?
Section titled “How does strict checking differ from normal?”Strict checking catches additional issues like old Git file modes with g+w bit that modern Git would flag as problems.
When should I use fsck?
Section titled “When should I use fsck?”Use fsck after repository corruption suspicions, before important operations, or as part of regular repository maintenance.
Can fsck fix corruption?
Section titled “Can fsck fix corruption?”fsck only detects corruption; it doesn’t repair it. For repair, you may need git fsck results to guide manual recovery or git-repair tools.
How do I check alternates and packed archives?
Section titled “How do I check alternates and packed archives?”To check all object locations including alternates and packs, use —full (default):
git fsck --fullApplications of the git fsck command
Section titled “Applications of the git fsck command”- Detecting repository corruption and integrity issues
- Cleaning up orphaned objects and unreachable commits
- Verifying repository health before critical operations
- Troubleshooting strange Git behavior or missing objects
- Preventive maintenance of important repositories
- Debugging synchronization issues between repository copies