How to Use git fsck to Check Repository Integrity
Use
git fsck to check the integrity of your Git repository by verifying objects and links. Running git fsck without options scans for corrupted or missing objects and reports any issues found.Syntax
The basic syntax of git fsck is simple and can be extended with options:
git fsck: Checks the repository for corrupted or missing objects.git fsck --full: Performs a full check including unreachable objects.git fsck --no-reflogs: Skips checking reflog entries.git fsck --lost-found: Saves dangling objects into.git/lost-foundfor recovery.
bash
git fsck [--full] [--no-reflogs] [--lost-found]
Example
This example runs git fsck on a repository to find any corrupted or missing objects. It shows how to interpret the output.
bash
git fsck
Output
Checking object directories: 100% (256/256), done.
Checking objects: 100% (1234/1234), done.
missing blob 3a1b2c4d5e6f7a8b9c0d1e2f3g4h5i6j7k8l9m0
error: object 3a1b2c4d5e6f7a8b9c0d1e2f3g4h5i6j7k8l9m0 is missing
Common Pitfalls
Common mistakes when using git fsck include:
- Running it on a bare or incomplete repository can show many errors that are expected.
- Ignoring warnings about dangling commits or blobs which might indicate lost work.
- Not using
--lost-foundto recover dangling objects. - Assuming
git fsckfixes problems automatically; it only reports issues.
bash
git fsck # Wrong: expecting automatic fix # Right: use output to manually recover or fix issues # For example, recover dangling objects: git fsck --lost-found
Quick Reference
| Option | Description |
|---|---|
| --full | Check all reachable and unreachable objects |
| --no-reflogs | Skip checking reflog entries |
| --lost-found | Save dangling objects to .git/lost-found |
| --strict | Report all errors strictly |
| --unreachable | Show unreachable objects |
Key Takeaways
Run
git fsck to verify the integrity of your Git repository objects.Use
--lost-found to recover dangling or lost objects manually.git fsck only reports problems; it does not fix them automatically.Check output carefully to identify missing or corrupted objects early.
Avoid running
git fsck on incomplete repositories to reduce false errors.