Appearance
Git Object Storage: Blobs, Trees, Commits, and Packfiles
Every commit, every file version, every directory listing — Git stores them all in a content-addressable object database. Understanding how this database works turns Git from a black box into a predictable system. This guide covers the four object types, loose-object storage, packfile compression, and the plumbing commands that inspect them.
The four object types
Git has exactly four object types. Everything in .git/objects is one of these:
| Type | Stores | SHA-1 input | Example size |
|---|---|---|---|
| blob | File content | "blob <size>\0<content>" | 100 bytes for a small file |
| tree | Directory listing (filenames, modes, hashes) | "tree <size>\0<entries>" | 500 bytes for 50 files |
| commit | Snapshot metadata (tree hash, parent, author, message) | "commit <size>\0<content>" | 300 bytes |
| tag | Annotated tag metadata | "tag <size>\0<content>" | 200 bytes |
The content-addressable property: the hash of every object is derived from its type, size, and content. Two files with identical content produce the same blob hash, no matter where they appear in the repository.
Loose object storage
New objects start as loose files in .git/objects/XX/YYYYYY...:
.git/objects/
a1/
b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0
ff/
1234567890abcdef0123456789abcdef0123456The first two hex characters are the directory name. The remaining 38 characters are the filename. The file itself is zlib-deflated. You can read any object with git cat-file:
bash
# Inspect a blob
git cat-file -p a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0
# Show the object type
git cat-file -t a1b2c3d
# Show the object size in bytes
git cat-file -s a1b2c3dTo see the raw deflated content without Git's pretty-printing:
bash
python3 -c "import zlib,sys; print(zlib.decompress(open('.git/objects/a1/b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0','rb').read()))"The hash function
Git uses SHA-1 by default, with SHA-256 support via --object-format=sha256 in new repositories (SHA-256 repos set extensions.objectFormat in config). The same object hashed with SHA-1 versus SHA-256 produces different hashes, which means SHA-256 repositories cannot directly interoperate with SHA-1 repositories.
The transition to SHA-256 is ongoing. As of Git 2.42, interop between SHA-1 and SHA-256 repos requires git protocol-v2 and compatible servers. Most repositories still use SHA-1. Check your repo:
bash
git config --get extensions.objectFormat
# (no output = SHA-1)Blob objects
A blob stores file content and nothing else. No filename, no mode, no metadata. The same content always produces the same blob hash:
bash
# Both files have identical content
echo "hello" > a.txt
echo "hello" > b.txt
git hash-object a.txt
# ce013625030ba8dba906f756967f9e9ca3944648
git hash-object b.txt
# ce013625030ba8dba906f756967f9e9ca3944648git hash-object computes the hash without adding the object to the database. Pass -w to write it:
bash
git hash-object -w a.txtThis is how Git deduplicates storage. A 100MB binary that appears in 10 branches stores exactly one blob object.
Tree objects
A tree maps filenames to blob (or subtree) hashes. Every entry stores:
- File mode (100644 for regular files, 100755 for executable, 040000 for trees)
- Object type (blob or tree)
- SHA-1 hash
- Filename
bash
# View the tree that HEAD points to
git cat-file -p HEAD^{tree}Output:
100644 blob a1b2c3d README.md
100755 blob e5f6a7b scripts/deploy.sh
040000 tree 9c8d7e6 srcThe src/ entry points to another tree object. git ls-tree shows the same data in a more scriptable format:
bash
git ls-tree HEAD
git ls-tree -r HEAD # recursiveTrees are content-addressed like blobs. Two directories with the same files and content produce the same tree hash. This is how Git detects unchanged directories without comparing every file.
Commit objects
A commit is a text record that points to a tree and zero or more parent commits:
tree a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0
parent 9b8c7d6e5f4a3b2c1d0e9f8a7b6c5d4e3f2a1b0c
author Jane Doe <jane@example.com> 1744675200 -0500
committer Jane Doe <jane@example.com> 1744675200 -0500
Add payment retry logicView any commit:
bash
git cat-file -p HEADThe commit object stores the author and committer separately because they can differ (rebase changes the committer, not the author). The timestamp is Unix epoch seconds plus timezone offset.
A merge commit has two or more parent lines. The initial commit (root commit) has no parent line.
Annotated tag objects
An annotated tag points to a commit (or another object) and stores the tagger, message, and optionally a GPG signature:
object a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0
type commit
tag v2.0.0
tagger Jane Doe <jane@example.com> 1744675200 -0500
Release v2.0.0: payment retry and UI fixesLightweight tags skip the tag object entirely — they are just a ref pointing directly at a commit.
Packfiles: where objects live long-term
Loose objects are simple but wasteful. Each object is a separate file, zlib-compressed. After a few thousand commits, loose objects waste inodes and disk space. Git packs them into packfiles (.pack + .idx):
.git/objects/pack/
pack-a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0.pack
pack-a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0.idx
pack-ff1234567890abcdef0123456789abcdef0123456.pack
pack-ff1234567890abcdef0123456789abcdef0123456.idxA packfile contains multiple objects in a single file. The .idx file is an index mapping object hashes to offsets inside the .pack. Git uses packfiles automatically during git gc, git repack, and git pack-objects.
Deltification
Packfiles compress further using delta encoding. Instead of storing each version of a file as a full blob, Git stores one base object and a series of delta objects that describe how to reconstruct the others:
Base blob: v1 of payment.js (25 KB)
Delta 1: v2 changes (1.2 KB) — "add try/catch around line 47"
Delta 2: v3 changes (400 B) — "rename retryCount to attempts"A delta stores: "at offset X, delete Y bytes, insert Z bytes." Git's packfile heuristics pick the best base objects to minimize total size. The result is typically 30-50% smaller than storing each version independently.
Delta chain depth
Each delta applies to the previous delta, forming a chain. Deep chains hurt read performance because Git must walk the entire chain to reconstruct the base object. git repack -a -d --window=250 --depth=50 limits chain depth to 50. The default depth is 50. Values above 250 rarely save space but always hurt reads.
Packfile inspection
bash
# Count objects in a pack
git count-objects -v
# Show packfile statistics
git verify-pack -v .git/objects/pack/*.idx | head -20
# Find which packfile contains a specific object
git -c pack.window=0 cat-file --batch-check="%(objectname) %(objectsize) %(deltabase)" < <(echo a1b2c3d)
# List all objects with their type and pack offset
git cat-file --batch-all-objects --unordered --buffer --batch-check="%(objecttype) %(objectname)"git count-objects -v output:
count: 42
size: 128
in-pack: 15420
packs: 3
size-pack: 48212
prune-packable: 0
garbage: 0
size-garbage: 0This tells you 42 loose objects exist, but 15,420 objects are packed across 3 packfiles totaling 48 MB.
Object reachability and garbage collection
Not every object in .git/objects is reachable. Orphaned commits, detached-HEAD experiments, and abandoned branches leave objects behind. git gc (garbage collection) does:
- Collects all objects reachable from refs (branches, tags, HEAD, reflog)
- Packs them into a single packfile
- Removes loose objects that are now in the pack
- Deletes unreachable objects older than
gc.pruneExpire(default 2 weeks)
bash
# Manual garbage collection
git gc
# Aggressive: re-delta everything (slow, space-hungry run)
git gc --aggressive
# Prune unreachable objects now (skip the grace period)
git gc --prune=nowThe 2-week grace period prevents data loss: if you accidentally delete a branch, the reflog keeps the commits reachable for 30 days, and git gc keeps them for another 14 days before actually deleting the objects.
Object storage on disk: summary
| Storage format | Location | Compression | When created |
|---|---|---|---|
| Loose object | .git/objects/XX/YYYYYY | zlib per-object | Every git add, git commit |
| Packfile | .git/objects/pack/*.pack | zlib + delta chains | git gc, git repack, git push |
| Pack index | .git/objects/pack/*.idx | None (sorted hashes + offsets) | Same as packfile |
| Multi-pack index | .git/objects/pack/multi-pack-index | None | git multi-pack-index write |
Edge cases
| Situation | What happens to storage |
|---|---|
| 1000 identical files in one commit | One blob object, one tree entry per file |
| Rename a file without changing content | New tree entry, same blob hash, no new blob |
| Amend a commit | New commit object, possibly new tree objects, same blobs for unchanged files |
| Rebase 50 commits | New commit objects for every rebased commit. Old commits become unreachable until git gc prunes them |
git filter-repo rewrites history | Every changed commit and tree gets a new hash. Old objects accumulate as garbage |
Quick reference
| Command | What it shows |
|---|---|
git cat-file -p <object> | Pretty-print any object |
git cat-file -t <object> | Object type |
git cat-file -s <object> | Object size in bytes |
git hash-object <file> | Compute blob hash without storing |
git hash-object -w <file> | Compute and store blob hash |
git ls-tree <tree-ish> | List tree entries |
git count-objects -v | Loose and packed object statistics |
git verify-pack -v <pack.idx> | List every object in a packfile with size, type, and delta base |
git gc | Garbage collect and repack |
git gc --aggressive | Full re-delta (slow, for repos with terrible packing) |
git repack -a -d | Repack all objects into a single packfile |
Pitfalls
git gc --aggressiveis rarely worth the CPU cost. The defaultgit gcproduces packs within 1-2% of optimal. Aggressive repacking takes hours on large repos for negligible space savings. Use it only before archiving a repository.- Delta chains longer than 50 hurt clone performance. The receiving end must reconstruct every delta. Deep chains mean slow initial clones. Run
git repack -a -d --depth=50if your packfile has chains exceeding 100. - Loose objects are not corruption-safe. If a loose object's file is truncated, Git detects the corruption on read (zlib checksum). Packfiles have a CRC32 per entry in the
.idxfile, plus a SHA-1 of the entire packfile. Rungit fsckto verify object integrity. - Bitmaps and multi-pack-indexes are separate from packfiles. A missing or stale
multi-pack-indexcausesgit fetchto scan all packfiles linearly instead of using the MIDX bitmap. If your repo has 20 packfiles and clones are slow, rebuild the MIDX:git multi-pack-index write. - Object storage is append-only until
git gc. Every commit, everygit add, everygit fetchadds objects. Deleted branches do not free disk space. Rungit gcperiodically or configuregc.auto(default: 6700 loose objects triggers auto-gc).