Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Maintenance and Recovery

git fsck command

Reference for git fsck: check the object database for corruption, missing objects and dangling commits, and learn how to recover lost work with --lost-found.

git fsck (file system check) walks the object database and verifies that it is healthy. It checks that every object can be read, that its content matches its ID, that the objects it refers to exist, and that trees, commits and tags are well formed. It also reports dangling objects: objects that nothing points to any more.

Most of the time you run it for one of two reasons. Either you suspect corruption (a crashed disk, an interrupted copy, an error such as "bad object"), or you want to find a commit that you lost after a reset or a deleted branch and that the reflog no longer remembers. In a healthy repository it prints nothing and exits with status 0.

Terminal
git fsck [options] [<object>...]

Try it

Bash Find a dangling commit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Detect a missing object

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git fsck [--full | --no-full] [--connectivity-only] [--strict]
         [--unreachable] [--dangling | --no-dangling] [--root] [--tags]
         [--cache] [--no-reflogs] [--lost-found] [--name-objects]
         [--progress | --no-progress] [--verbose] [--[no-]references]
         [<object>...]
Name Description
--full Check loose objects, packs and alternates. This is the default; --no-full skips packs and alternate object stores.
--connectivity-only Only check that all reachable objects exist and are connected. It does not unpack blob contents, so it is much faster on big repositories.
--strict Apply stricter rules, such as zero-padded file modes in trees, which older Git versions could write.
--unreachable Print every object that is not reachable from any ref, not only the tips of unreachable chains.
--dangling / --no-dangling Print (default) or hide dangling objects.
--no-reflogs Do not treat reflog entries as reachable. Without it, a commit that the reflog still remembers is not reported.
--lost-found Write dangling commits and blobs to .git/lost-found/commit/ and .git/lost-found/other/.
--root Report root commits (commits without parents).
--tags Report the tags found.
--cache Also treat objects recorded in the index as reachable.
--name-objects Show a path-like name such as HEAD~2^2:file.txt next to each object ID in error messages.
--references / --no-references Check (default) or skip the validity of refs.
<object>... Start the reachability walk from these objects instead of all refs.

Returns: exit status 0 when no problems were found; a non-zero status when errors were found. Dangling objects alone do not make the status non-zero.

Errors and edge cases: a missing object is reported as missing blob <id> (or tree, commit) and gives exit status 2, as in the second example; other damage can add broken link from ... to ... lines. A corrupt loose object gives error: ... : object corrupt or missing or a hash mismatch message.

Usage notes

  • Dangling is not broken. After git reset --hard, git commit --amend or a rebase you will always see dangling commits. They are the old versions, waiting for git gc to remove them after the expiry time.
  • Unreachable versus dangling. A dangling object has no incoming reference at all. An unreachable object cannot be reached from a ref but may be referenced by another unreachable object. By default only the tips of such chains are printed as dangling; --unreachable prints all of them.
  • Reflog counts as a root. Commits that the reflog remembers are not reported unless you pass --no-reflogs. Use that flag when you search for lost work.
  • Recovering a commit. Take the ID from the output, inspect it with git show <id>, then give it a name with git branch rescue <id>. --lost-found automates the collection, but names nothing.
  • Cost. --full reads and decompresses every object, so it can take minutes on a large repository. --connectivity-only is the quick option for a periodic health check.
  • Hosting servers. Setting transfer.fsckObjects or receive.fsckObjects makes fetch and push run the same checks on incoming objects, which keeps broken data out in the first place. See transfer.fsckObjects.
  • Repair. fsck only reports. A missing object can sometimes be fetched again from a remote (git fetch), or copied from another clone. Never run git gc --prune=now on a repository you suspect is damaged.