Sub topic 14
Reachability, git fsck and garbage collection
This chapter assumes you can run the plumbing commands from the earlier chapters. You should know what blobs, trees, commits and refs are, and how loose objects differ from packfiles.
Reachability
Git never deletes an object when you stop using it. Objects are removed later, and only if nothing can reach them. An object is reachable if you can arrive at it by starting from a ref (a branch, a tag or HEAD) and following links. A commit links to its tree and parents. A tree links to its blobs and subtrees. An annotated tag links to the object it names.
The orphan blob has no incoming link. Git calls this kind of object unreachable. Git keeps unreachable objects for a grace period, so a mistake such as a dropped branch can still be recovered.
Creating an unreachable object
Write a blob and never stage or commit it:
orphan=$(echo 'orphan' | git hash-object -w --stdin)
git cat-file -t "$orphan"
git fsck --unreachablegit fsck walks the object database from every ref and from the reflog. It then reports what it could not reach. The output looks like this:
unreachable blob 9d4e0b5b0a4d5ab5e0d8f0f6c4a5d1f2c6a7b8c9The hash will differ in your repository. git fsck with no flags reports dangling blob ... instead. A dangling object is unreachable and also not referenced by any other unreachable object. --unreachable lists every unreachable object, including those that other unreachable objects point to.
Garbage collection
git gc packs loose objects into a packfile and removes objects that are old and unreachable. Compare the counts before and after:
git count-objects -v
git gc
git count-objects -v
git verify-pack -v .git/objects/pack/pack-*.idx
git fsck --unreachableAfter git gc, the count value (loose objects) drops, and in-pack rises. The verify-pack output lists each packed object with its type, size and offset. The orphan blob is still listed by fsck, though. A plain git gc only prunes unreachable objects older than gc.pruneExpire, which defaults to two weeks. The delay protects objects that another Git process may be about to link, and it gives you time to recover work.
To remove the orphan immediately, override the grace period:
git gc --prune=now
git fsck --unreachable
git cat-file -t "$orphan"fsck now prints nothing. The last command fails with an error such as fatal: git cat-file: could not get object info, because the object is gone.
Why reflogs keep objects alive
If you delete a branch, its commits become unreachable from refs. The reflog still records where HEAD and each branch pointed. git fsck and git gc treat reflog entries as roots, so those commits survive until the entries expire. The defaults are 90 days for reflog entries whose commits are still reachable from the branch, and 30 days for those that are not. To see what is truly unreachable, use git fsck --unreachable --no-reflogs.
Common mistakes
- Expecting
git gcto clear unreachable objects at once. The two-week grace period prevents that. - Reading
danglingandunreachableas the same thing. Dangling is the narrower set. - Forgetting the reflog. A commit that seems orphaned may still be reachable through it.
- Running
git verify-packbefore any pack exists. The glob matches nothing untilgit gcor a repack has created a pack.
Exercise
You delete a branch with git branch -D feature, then run git gc. Is the branch's last commit gone? Give two reasons it may survive.
Show answer
Usually it survives. First, the reflog still references the commit, and gc treats reflog entries as roots until they expire. Second, even if nothing references it, a plain git gc only prunes unreachable objects older than gc.pruneExpire (two weeks by default). To find it, use git fsck --unreachable --no-reflogs or git reflog.