4. Pack Files & the Reflog

How Git keeps a million tiny objects from melting your disk — and how it remembers every ref you've ever touched

The content-addressed object store is conceptually beautiful but operationally wasteful. Every time you edit a 500-line file and commit, Git stores a new blob containing the whole file. Change one character in a 10 MB file and you've written 10 MB to .git/objects/. Over thousands of commits, this adds up to an absurd amount of disk space.

Except it doesn't. Run du -sh .git on any active repo and you'll be surprised how small it is. That's because of pack files — the other half of Git's storage system, where it trades simplicity for devastating efficiency.

Loose objects vs packed objects

Every object in Git exists in one of two storage formats:

Loose

One file per object in .git/objects/<ab>/<cdef…>. Written as soon as the object is created. Easy to read (decompress, parse header), easy to write (hash, deflate, write).

Packed

Many objects crammed into a single file under .git/objects/pack/pack-<hash>.pack, with a companion .idx file for lookups. Compressed together using delta compression against other objects. Far smaller.

New objects always start as loose objects — it's fast and simple. When the loose collection grows large (default: 6,700 objects), or when you git push / git gc, Git runs git repack, which consolidates everything into pack files and deletes the loose originals.

Delta compression — the real magic

Inside a pack file, objects are stored in one of two ways: as whole objects (zlib-compressed, same as loose) or as deltas — instructions for reconstructing this object from another one.

A delta looks roughly like: "start with object X, keep bytes 0-4190, then insert these 47 bytes, then copy bytes 4238-10000 from X, then insert these 3 bytes." If two versions of a file are 99% identical, the delta is tiny. The first version is stored whole; every subsequent version is a delta chain back to it.

Why this is different from storing diffs

CVS, SVN, and older systems stored textual diffs between versions. Git doesn't — the object store is conceptually full-snapshot. Delta compression is a storage optimization layer that kicks in only when packing. Each object is logically whole; its bytes are just assembled on-the-fly from a delta chain. This separation of logical vs physical storage is why Git commands work uniformly — nothing above the object-store layer needs to know whether a blob came from a loose file or a 17-deep delta chain.

Git picks what to delta against automatically

Here's the clever part. Git doesn't assume "this version follows that version" from commit order. Instead, during git repack, it:

  1. Groups objects by type (blobs with blobs, trees with trees).
  2. Sorts within each group by a heuristic: path, size, recency.
  3. For each object, tries deltifying it against a handful of "similar" candidates in the sorted window.
  4. Keeps the smallest delta (or keeps the whole object if no delta beats a threshold).

The payoff: a blob that looks nothing like its chronological predecessors, but does look like some other file in a different directory, will still get deltified efficiently. Git optimizes for content similarity, not history order.

The on-disk shape of a pack file

A pack is two files that always come as a pair:

.git/objects/pack/ ├── pack-af7c9…3d.pack ← the actual object data └── pack-af7c9…3d.idx ← hash → offset lookup table

The .pack file is a header, then a stream of object entries, then a 20-byte checksum. Each entry is prefixed with a variable-length header encoding its type (one of the standard types, plus OFS_DELTA and REF_DELTA for the two delta forms) and the inflated size. The payload is either raw zlib-compressed bytes (for whole objects) or a delta instruction stream against a base object identified by either a pack-relative offset (OFS_DELTA) or a full hash (REF_DELTA).

The .idx file is what makes lookups fast. It maps object hashes to their byte offsets within the .pack. The index has a fanout table keyed by the first byte of the hash — so to find an object, Git reads one fanout entry, then binary searches within the narrow range. Looking up any of a million objects costs ~20 bytes of I/O.

$ git verify-pack -v .git/objects/pack/pack-af7c9…3d.idx 3e8f4c7a… commit 241 166 offset 12 7a9f31e2… tree 137 90 offset 178 af5626b4… blob 14 22 offset 268 ← stored whole 1b2c3d4e… blob 4188 128 offset 290 1 7a9f31e2… ← delta vs 7a9f…, chain depth 1 c3b8f4e1… blob 12240 96 offset 418 2 1b2c3d4e… ← delta of a delta … non-delta: 47 objects chain length = 1: 312 objects chain length = 2: 94 objects chain length = 3: 22 objects

The last few lines are telling: most objects are one or two steps removed from a whole base object. Git caps delta chain depth (default 50) to keep reconstruction fast — if you have to apply 50 deltas in sequence to get a 12 KB blob, you might as well have stored it whole.

Garbage collection: when loose objects become pack objects

git gc is the housekeeper. It:

  • Packs loose objects into a fresh pack file via git repack.
  • Deletes loose objects that are now in the pack.
  • Expires reflog entries older than 90 days (configurable).
  • Deletes unreachable objects older than 2 weeks (gc.pruneExpire).
  • Packs refs into .git/packed-refs.

Git runs gc --auto quietly after many commands if the loose object count crosses the threshold. You rarely need to invoke it by hand. The most interesting part for our purposes is how reachability is decided — because that's where the reflog comes in.

Reachability: what makes an object "live"

Garbage collection deletes objects that nothing points at. The starting set of "roots" is every ref in the repository — every branch, every tag, every remote-tracking ref, HEAD — plus the reflog. From each root, Git walks parent pointers, tree-entry hashes, and blob references, marking everything it can reach. Whatever didn't get marked is unreachable and eligible for deletion (after the grace period).

So if you rewrite a branch and throw away commits, those commits are orphaned from a branch's perspective but still reachable through the reflog — which is exactly why the reflog exists.

The reflog: Git's undo buffer

Every time a ref moves — every commit, checkout, reset, rebase, merge, amend — Git appends a line to that ref's reflog, recording where it was and where it went, along with a timestamp and a short description.

$ git reflog 3e8f4c7 HEAD@{0}: commit: add user validation 1b2c3d4 HEAD@{1}: commit: refactor auth module 7a9f31e HEAD@{2}: reset: moving to HEAD~3 af5626b HEAD@{3}: rebase (finish): returning to refs/heads/feature c3b8f4e HEAD@{4}: rebase (start): checkout main d7e8f9a HEAD@{5}: checkout: moving from main to feature e1f2a3b HEAD@{6}: commit (amend): fix typo in commit message …

These entries live in two places:

  • .git/logs/HEAD — HEAD's personal reflog (what you see with git reflog alone).
  • .git/logs/refs/heads/<branch> — one reflog per branch (git reflog show feature).

Each line is a plain-text record: <old-hash> <new-hash> <author> <timestamp> <description>. You can cat it. It's as approachable as everything else in Git.

The reflog is why you almost can't lose work in Git

Any commit you ever made — even commits you "threw away" by rebasing or resetting — stays reachable through the reflog for 90 days (30 for unreachable commits by default). It's the safety net beneath every destructive operation. We'll lean on it hard in the recovery article.

Reflog syntax works in any ref expression

Anywhere you can use a commit hash or branch name, you can use a reflog reference:

SyntaxMeaning
HEAD@{0}Current HEAD (most recent reflog entry).
HEAD@{5}Where HEAD was 5 moves ago.
HEAD@{yesterday}Where HEAD was roughly 24h ago.
main@{1.week.ago}Where main was a week ago.
@{-1}The previously-checked-out branch.

So git diff HEAD@{1} shows what changed between the previous HEAD and now — incredibly handy after a confusing rebase.

The storage pipeline, end to end

Working tree editable files Index (staging) .git/index Loose objects .git/objects/ab/cdef… Pack file .pack + .idx git add git commit git gc / push

Every stage writes content-addressed objects that the next stage consumes. The index (also called the staging area or cache) lives at .git/index — a binary file listing what tree would be produced if you committed right now. When you commit, that tree is written into the object store, a commit object is created pointing at it, and the current branch's ref is updated.

Try It Yourself — Delta Compression

Edit the "base" and "new version" below. The visualizer computes the delta instructions Git would use to reconstruct the new version from the base — copy unchanged regions, insert the changed parts. Watch the delta size stay tiny even for large files with small edits.

Delta instructions

Visual diff

Compression stats

What to take away

  • Loose objects are convenient; pack files are efficient. git gc moves things between them.
  • Delta compression stores similar blobs as small diffs against each other — chosen by content similarity, not commit order.
  • The reflog records every ref movement. It's why "destructive" Git operations are almost always reversible.
  • Every moving part — loose objects, packed objects, refs, reflog — is a plain file or a straightforward binary format. No hidden database.