What is a Packfile
A packfile (.git/objects/pack/*.pack) bundles many Git objects into a single file, using delta compression to store similar objects efficiently. Packfiles replace loose objects to save disk space and speed up network transfers.
How it works
When Git creates a packfile (during git gc, git push, or git fetch), it:
- Sorts objects by type and size.
- Finds similar objects (e.g., different versions of the same file).
- Stores one as a base object and the others as binary deltas against the base.
- Writes everything into a single
.packfile with a companion.idxindex.
A 10 KB file that changes by 50 bytes across 100 commits stores one full copy (10 KB) plus 99 deltas (a few KB total) instead of 100 full copies (1 MB).
The pack index maps SHA-1 hashes to byte offsets in the packfile. Finding any object is a binary search on the index — O(log n) — followed by a seek in the packfile.
Delta chains can nest: object A is a delta of B, which is a delta of C. Git limits chain depth (default 50) to keep decompression fast.
Why it matters
Packfiles are why .git directories are small despite storing complete snapshots at every commit. They are also why git clone and git fetch transfer data efficiently — the server sends a packfile containing only objects the client lacks.
See How Git Internals Work for the full explanation of packfiles, loose objects, and garbage collection.