git,

Git 101: loose vs dangling vs unreachable objects

Nov 03, 2022 · 1 min read · Post a comment

You can find Git project objects under .git/objects. When dealing with managing objects using git fsck and git gc commands, you might stumble upon the following types of “failed” objects: loose, dangling and unreachable. Let’s see what’s that all about.

Prerequisites

  • Git

Solution

Loose object is every Git object which is stored under a two-digit subdirectory under .git/objects. For instance a loose object is the following one: .git/objects/1f/c0c6a84445fe47117673b09a3d4e05ed5b4325.

Basically these objects are still not compressed together under a single file.

Unreachable object is a Git object which is inaccessible from any other commit, tag or reference. To list unreachable objects, run: git fsck --unreachable.

Dangling object is an unreachable Git object that’s not pointed at (referenced) by anything including other unreachable objects. To recap, a dangling object = unreachable + no one points to it.

To list dangling objects, run: git fsck --dangling. Since a dangling object could be commit or blob as well, to list the dangling commits only, run:

git fsck --lost-found | grep "^dangling commit" | sed

Note(s): Adding option --lost-found will save dangling commits under .git/lost-found/commit. Blobs are stored under .git/lost-found/other though.

Now, bare with me. A loose object can be unreachable or dangling too.

If you want to get rid of from all these objects run: git gc --prune=now. Related: Git gc.

Conclusion

To find more neat Git commands and hacks, simply browse the Git category. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.

git