git-fsck
Check Git repository integrity
TLDR
Check the current repository
List all tags found
List all root nodes found
Show all unreachable and dangling objects, ignore reflogs, and perform a full integrity check
Check connectivity only (skip object integrity verification)
SYNOPSIS
git fsck [--full] [--unreachable] [--dangling] [--lost-found] [--no-reflogs] [--strict] [--connectivity-only] [--root] [--tags] [--cache] [--objects] [--indexed-objects] [--name-objects] [--progress] [--no-progress] [object_id...]
PARAMETERS
--full
Check all objects accessible from reachable refs, plus all objects in the object database. This is typically what you want for a comprehensive check.
--unreachable
Print objects that are unreachable from any known branch or tag.
--dangling
Print objects that are dangling (not referenced by any other object, but still valid).
--lost-found
Write unreachable and dangling objects into .git/lost-found/commit/ or .git/lost-found/other/ respectively.
--no-reflogs
Do not consider reflogs when determining reachability.
--strict
Check objects more strictly, reporting additional errors.
--connectivity-only
Only check connectivity, not validity of content.
--root
Report root commits that are not reachable from any ref.
--tags
Report tags that do not point to a valid object.
--cache
Consider objects in the index as reachable.
--objects
Limit output to objects only, not references.
--indexed-objects
Only consider objects that are mentioned in the index.
--name-objects
Show human-readable names for objects if possible.
--progress
Show progress during the check.
--no-progress
Do not show progress during the check.
DESCRIPTION
git-fsck (file system check) is a Git command used to verify the connectivity and validity of the objects in the Git object database. It scans the repository's objects to find corrupted or unreachable objects, ensuring that the repository's internal structure is sound.
This command is crucial for maintaining the health and integrity of a Git repository, especially after unexpected system crashes or disk errors. It can identify dangling objects (objects not referenced by any commit or branch), unreachable objects (objects that are still valid but not reachable from any known branch or tag), and corrupted objects. git-fsck is a read-only operation; it does not modify the repository.
CAVEATS
git-fsck is a diagnostic tool only. It reports issues but does not fix them. For example, it will identify corrupted objects but will not attempt to repair them. To recover objects, manual intervention or other Git commands like git restore (if in index) or git replace may be needed, often after manually inspecting objects found in lost-found. It's also important to note that git-fsck can be resource-intensive on very large repositories.
USE IN RECOVERY
git-fsck is often the first step in diagnosing repository corruption or data loss. By identifying dangling or unreachable objects, it provides clues that can be used with git-cat-file and git-reset or git-cherry-pick to potentially recover lost work.
READ-ONLY NATURE
Emphasizing its read-only nature is crucial. It gives confidence that running the command won't worsen any existing problems in the repository, making it safe to use for initial diagnosis.
HISTORY
git-fsck has been a fundamental component of Git since its early days, serving as the primary integrity checker for the object database. Its design reflects Git's core principle of data integrity, allowing users to verify the internal consistency of their repositories. Its evolution has focused on adding more diagnostic options and improving performance rather than changing its core function.
SEE ALSO
git gc(1), git reflog(1), git prune(1), git cat-file(1)