Sub topic 14
Reachability, git fsck and garbage collection
This chapter assumes you have followed the earlier chapters. You should have a repository built with plumbing commands that holds a blob, trees, two commits, the branch main and the annotated tag v1. You should also know that loose objects live under .git/objects/xx/ and that git gc creates packfiles.
Git rarely deletes anything when you run a command. A reset, a deleted branch or an amended commit only moves a ref, and the old objects stay in the database. This chapter explains when those objects count as garbage, how to find them with git fsck, and when git gc finally removes them.
What reachable means
An object is reachable if you can get to it by starting at a root and following links. Roots and links work like this:
- Roots are refs: branches in
refs/heads, tags inrefs/tags, remote-tracking branches inrefs/remotes, andHEAD. Entries in the reflogs also count as roots. So do objects recorded in the index. - A commit links to its tree and to its parent commits.
- A tree links to its blobs and to its subtrees.
- An annotated tag links to the object it points at.
An object that no root leads to is unreachable. It is still a valid object and can still be read by hash. It is just not part of any history Git will show you.
Finding unreachable objects with git fsck
git fsck has two jobs. It checks that every object is well formed and that every link points at an object that exists. It also reports objects that no root reaches. Run it on the repository from the earlier chapters:
git fsckA healthy repository prints nothing, or only notes such as Checking object directories. Now create an object that nothing refers to:
orphan=$(echo 'nobody points at me' | git hash-object -w --stdin)
git fsck
git fsck --unreachableThe first git fsck prints a line like this:
dangling blob 3f1c0e5a...A dangling object is unreachable and also not referenced by any other unreachable object. It is the tip of a lost chain. git fsck --unreachable is broader. It lists every unreachable object, including the ones that dangling objects point to. For a lone blob the two lists are the same. For an abandoned commit, fsck shows the commit as dangling. --unreachable also lists the commit's tree and blobs, unless something reachable still uses them.
| Command | What it reports |
|---|---|
| `git fsck` | Corruption and missing objects, plus dangling objects |
| `git fsck --unreachable` | Every object that no root reaches |
| `git fsck --no-reflogs` | Treats reflog entries as non-roots, so commits kept alive only by the reflog show up |
| `git fsck --lost-found` | Writes dangling commits and blobs into `.git/lost-found/` so you can look at them |
A missing object is different from a dangling one. A message such as missing blob or broken link from tree ... to blob ... means something points at an object that is not in the database. That is real damage, not garbage.
How an object becomes unreachable
Two common ways to lose a commit are deleting a branch and moving a branch with git reset. Try the first:
git switch -c scratch
git commit --allow-empty -m 'experiment'
lost=$(git rev-parse HEAD)
git switch main
git branch -D scratch
git fsck
git fsck --no-reflogsThis example needs user.name and user.email to be set. After git branch -D scratch, no branch reaches the experiment commit. The plain git fsck still prints nothing about it. The reason is that the reflog of HEAD remembers that you were on that commit, and fsck treats reflog entries as roots. git fsck --no-reflogs ignores them and should print dangling commit followed by the value of $lost.
This is why you can usually recover a "lost" commit. As long as the object exists, you can point a ref back at it:
git branch rescued "$lost"Why unreachable objects stay around
Git keeps unreachable objects for three reasons:
- The reflog is a safety net. It records where each ref used to point. Entries expire after 90 days if their commits are still reachable and after 30 days if they are not. The settings are
gc.reflogExpireandgc.reflogExpireUnreachable. - Concurrency. Another Git process may have just written an object and not yet created the ref that uses it. If
gcdeleted all unreachable objects at once, it could delete that one. - Cost. Walking every root to find garbage takes time. Git does it only during
gcand similar maintenance.
For the concurrency reason, git gc only prunes unreachable objects older than a grace period. The setting is gc.pruneExpire, and the default is two weeks. A new orphan blob is kept even if you run git gc immediately.
Loose objects and packfiles during gc
git gc does several jobs. It packs loose objects into a packfile, packs refs, expires old reflog entries, and prunes old unreachable loose objects. Check the effect on the repository from this tutorial:
git count-objects -v
git gc
git count-objects -v
git verify-pack -v .git/objects/pack/pack-*.idx
git fsck --unreachableIn the count-objects -v output, count is the number of loose objects and in-pack is the number of objects in packfiles. After git gc, count should drop and in-pack should rise. Your orphan blob is still loose, because it is unreachable and younger than two weeks. git fsck --unreachable should still list it.
verify-pack -v prints one line per packed object with its hash, type, size and offset. Some entries are stored as deltas against other objects. Those lines also show a delta depth and the hash of the base object.
Unreachable objects are not always loose. If a repack runs after an object became unreachable, and that object is already inside a pack, Git can write it back out as a loose object. Its file modification time then starts the grace period. Newer Git versions can also store such objects in a separate cruft pack. Either way, you cannot tell from the object's location whether it is garbage. Only reachability tells you.
Forcing garbage collection
To delete the objects from the earlier examples now, you must remove both protections. These are the reflog entries and the grace period:
git reflog expire --expire-unreachable=now --all
git gc --prune=now
git cat-file -t "$lost"
git cat-file -t "$orphan"
git fsck --unreachableThe reflog expiry removes the entries that kept the experiment commit alive. --prune=now sets the grace period to zero. After that, both git cat-file -t commands should fail with a message saying the object could not be found. git fsck --unreachable should print nothing.
Common mistakes
- Expecting
git gcto free space at once. The reflog and the two-week grace period both keep objects. Run the two commands from the previous section to remove them. - Treating
danglingas an error. Dangling objects are normal after amends, rebases and deleted branches. The messages to worry about aremissing,broken link,corruptand hash mismatches. - Assuming a plain
git fsckshows all lost commits. It counts reflog entries as roots. Use--no-reflogsor--unreachableto see more. - Forgetting the index. Objects staged with
git update-indexorgit addare protected by the index even if no commit contains them. This is why a blob you staged but never committed does not appear as dangling.
Exercise
You delete a branch with git branch -D feature. Ten minutes later you run git gc and the commits from that branch are still readable with git cat-file -p <hash>. Name the two mechanisms that kept them. Which command with which options would remove them immediately?
Show answer
The reflog still has entries for those commits, so they count as reachable roots. Even without the reflog, git gc keeps unreachable objects younger than gc.pruneExpire (two weeks by default). To remove them now, run git reflog expire --expire-unreachable=now --all and then git gc --prune=now.
Exercise
git fsck prints dangling blob for one hash and missing blob for another. Which one means the repository is damaged, and why?
Show answer
missing blob means a tree refers to a blob that is not in the object database, so the history is broken. dangling blob only means no root reaches that blob, which is normal garbage that gc removes after the grace period.