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:
- Groups objects by type (blobs with blobs, trees with trees).
- Sorts within each group by a heuristic: path, size, recency.
- For each object, tries deltifying it against a handful of "similar" candidates in the sorted window.
- 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:
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.
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.
These entries live in two places:
.git/logs/HEAD— HEAD's personal reflog (what you see withgit reflogalone)..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:
| Syntax | Meaning |
|---|---|
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
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 gcmoves 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.