Git Reference ยท Objects, Refs and Internals
Loose object format
How Git stores one object per file: the type-size header, zlib compression, the two-level directory path from the object ID, and how to read a loose object by hand.
A loose object is the simplest way Git stores data: one file per object, under .git/objects. The file contains the object's header and content, compressed together with zlib. Every new blob, tree, commit and tag starts life this way; later git gc or a push moves them into packfiles.
The path comes from the object ID. The first two hexadecimal digits name a subdirectory and the rest is the file name, so the object 45b983beโฆ lives at .git/objects/45/b983beโฆ. Splitting on two digits keeps any single directory small, since there are at most 256 of them.
.git/objects/45/b983be36b73c0788dc9cbcb76cbb80fc7bb057
โโ first 2 digits โโ remaining 38 digits
file content = zlib( "blob 3" NUL "hi\n" )
Try it
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.
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.
Layout
Header. <type> <size> with the type being blob, tree, commit or tag, a single space, the content length in decimal bytes, then one NUL byte. The size counts only the content, not the header.
Content. The raw object body. For a blob it is the file bytes; for trees, commits and tags see Tree objects, Commit objects and Tag objects.
Compression. The header and content are concatenated and compressed as a single zlib stream (RFC 1950), which by default starts with the bytes 78 01 (Git favors speed for loose objects, as the first example shows). core.looseCompression (falling back to core.compression) sets the level; 0 stores without compressing.
ID. The object ID is the hash of the uncompressed bytes, header included. It does not depend on the compression level, so the same object compresses differently on different machines but keeps its ID. See Object IDs and SHA-256.
Permissions and writing. Git writes to a temporary file in the object directory, then renames it into place. Loose objects are created read-only (mode 0444) because they never change. core.sharedRepository adjusts group access for shared repositories.
Usage notes
- Not human readable:
caton the file prints binary noise. Usegit cat-file -p <id>, which also reads packed objects. - Count and size them:
git count-objects -vreports loose objects and their disk usage;git gcpacks them. A very large number of loose files slows file systems, which is whygc.autotriggers packing at around 6,700 of them. - Existence check:
git cat-file -e <id>exits 0 if the object exists, whether loose or packed. - Corruption: a truncated or damaged loose file produces
inflate: data stream errororobject file ... is empty.git fsckfinds it; restore the object from another clone withgit fetch, or copy the file back from a backup. - Unreachable loose objects (from amended commits, abandoned stashes,
git addof a file you then changed) stay for thegc.pruneExpireperiod, two weeks by default, which is what makes the reflog andfsck --lost-foundrecovery work. git hash-object -wwrites one directly, andgit unpack-objectsexplodes a pack into loose files; both are plumbing used mostly in tests and scripts.