arthrop0d

Brief articles on varied topics.

Git Packfiles, Delta Chains, and Repacks

gitalgorithms

Git packfiles are archives that store many Git objects together, often as compressed full objects or as deltas against similar objects. A delta records only how to transform one object into another, so Git can reconstruct the exact original object when it needs it.

The problem with loose objects

Git stores commits, trees, blobs, and annotated tags as objects. A blob contains file data; a tree describes a directory; a commit points to a tree and its parents; a tag names another object.

A newly created object usually starts as a loose object: one compressed file under .git/objects. Its path is derived from the object's content hash. For example, an object whose ID begins with a1b2... is stored below a directory named a1, with the remaining ID as its filename.

Before compression, Git gives each object a small header containing its type and size, followed by a zero byte and the object's content. The object ID is calculated from this complete representation. Git then compresses it with zlib.

Loose objects are simple and robust, but a repository with many commits can accumulate hundreds of thousands of tiny files. That creates overhead in directory traversal, filesystem metadata, backups, and system calls. Many versions of a source file also contain almost the same bytes, so compressing each version independently misses an important opportunity: storing the repeated bytes only once.

A packfile addresses both problems by putting many objects into a small number of large files and allowing related objects to share storage.

What a packfile contains

A pack is normally a file such as .git/objects/pack/pack-<name>.pack, accompanied by an index file, usually ending in .idx.

The pack begins with a header identifying the format, its version, and the number of stored objects. It then contains a sequence of object entries. Each entry identifies an object representation and stores compressed data. At the end, Git writes a checksum for the pack's contents, allowing corruption to be detected.

The index is the lookup structure. It maps an object's content hash to the byte offset of that object's entry in the pack. Without the index, finding one object would require scanning the entire pack. With it, Git can find the entry directly, seek to that offset, decompress it, and verify that the reconstructed bytes have the expected object ID.

Packing does not change Git's object model. A commit still refers to a tree by its hash, and a tree still refers to blobs and subtrees by their hashes. The pack is only a more efficient physical representation of the same objects.

A packfile is not one giant compressed stream. Its entries can be located independently, which is what makes indexed lookup practical.

Full objects and deltas

Some pack entries contain a complete object, including its type and uncompressed size. Other entries are deltas: instructions for producing an object from a similar base object.

A delta contains two sizes followed by instructions: the expected size of the base and the expected size of the result. An instruction either:

For example, if successive versions of a source file differ by one changed line, a delta can copy nearly the entire old file and insert the replacement bytes. The compressed delta is often much smaller than a separately compressed copy of the new file.

The base can be a complete object or another delta. Consequently, Git can store a chain such as:

full file version A
  -> delta producing version B
    -> delta producing version C

To reconstruct version C, Git first reconstructs B from A, then applies C's delta to B. The result is the exact uncompressed object content, not an approximation or a patch that must remain applied forever.

Pack entries use one of two common ways to name a delta base. An offset delta points backward to another entry in the same pack using its byte distance. A reference delta names the base by its object ID, allowing the base to be in another pack or to be found through the normal object database. Offset deltas are compact; reference deltas are more flexible.

Git limits practical delta-chain depth and can choose not to delta-compress some objects. Very long chains would save space but make reads and repacking slower, because reconstructing the final object requires more steps.

Why Git repacks

A repack reads objects from loose storage and existing packs, chooses a new arrangement, and writes one or more replacement packs and indexes. It commonly performs several useful jobs at once:

  1. Reduce filesystem overhead. Thousands of loose files become a pack and its index.
  2. Improve compression. Git can compare similar objects and create deltas between versions.
  3. Consolidate packs. Repeated fetches or incremental operations can leave several packs. Combining them reduces the number of places Git must search.
  4. Manage reachability. Git can identify objects reachable from references such as branches and tags, while retaining unreachable objects temporarily for recovery.

Commands such as git gc may invoke repacking as part of repository maintenance. A fetch can also create a new pack, which is why repositories sometimes accumulate multiple packfiles until maintenance consolidates them.

Repacking is not simply “compress everything again.” Git must preserve every object that references, reflogs, or other safety rules still make important, select suitable bases, write valid indexes, and atomically replace the old storage. Unreachable objects may eventually be pruned after a grace period, but repacking and pruning are separate concerns.

When a stack trace or maintenance log mentions a pack, idx file, delta, thin pack, or unpack objects, it is describing this storage layer. Git is still addressing objects by their hashes; packfiles are the compressed, indexed implementation that makes a large content-addressed history manageable.

← All articles