Packfiles and Garbage Collection
A packfile is a single file in which Git stores many objects together, compressed against each other. New objects are first written as loose objects, one compressed file each. From time to time, Git packs them: it puts them into a packfile and, for objects that are similar (such as two versions of the same file), stores only one in full and the others as deltas, the differences from it. Garbage collection (git gc) does this packing and also deletes unreachable objects that are old enough. This is why a repository with years of history is often smaller than you would expect.
Think of how you'd store twenty drafts of the same essay. Keeping twenty full copies wastes space. Keeping the final version in full, and for each older draft only a note of what was different, takes a fraction of the room, and you can still rebuild any draft.
Loose Objects
So that the output on this page is the same on every run, the repository first sets pack.threads to 1 (Git normally uses several threads, which can choose deltas in a slightly different order). On your computer you can leave that line out. Then Navika creates a fee list for 200 students and changes it in four commits. Inside double quotes, the shell would read $12 as a variable, so the backslash in \$12,000 keeps the dollar sign as plain text:
mkdir fee-tracker
cd fee-tracker
git init --quiet
git config pack.threads 1
for i in $(seq 1 200); do echo "Student $i: \$12,000"; done > fees.txt
git add fees.txt
git commit --quiet -m "Add the fee list"
for n in 1 2 3; do
sed -i.bak "s/^Student $n: .*/Student $n: \$12,500/" fees.txt
rm fees.txt.bak
git commit --quiet -am "Correct the fee of student $n"
done
git count-objects -v | grep -E "^(count|in-pack|packs):"count: 12
in-pack: 0
packs: 0Explanation: count is the number of loose objects: 12, three per commit (a blob, a tree and a commit). in-pack is 0 and there are no packs yet. Each of the four blobs is a complete copy of the 200-line file, compressed on its own, although the versions differ in a single line.
Packing: git gc
git gc --quiet
git count-objects -v | grep -E "^(count|in-pack|packs):"
ls .git/objects/packcount: 0
in-pack: 12
packs: 1
pack-d7e6424c433b2c7af54e9a42ed56a90d2e901d4a.idx
pack-d7e6424c433b2c7af54e9a42ed56a90d2e901d4a.pack
pack-d7e6424c433b2c7af54e9a42ed56a90d2e901d4a.revExplanation: the 12 objects moved into one pack, and no loose objects are left. A pack is two files: the .pack holds the objects, and the .idx is an index that finds any object in the pack quickly by its ID. (Newer Git versions also write a .rev file, a reverse index for faster lookups.)
Inside a Pack: git verify-pack
git verify-pack -v checks a pack and lists every object in it:
git verify-pack -v .git/objects/pack/*.idx | grep blob773c00f165a12de64455024482c7e26ae0539496 blob 4092 477 649
bfdd5456917ec1afd682a70a8a55743ae1f47636 blob 14 25 1220 1 773c00f165a12de64455024482c7e26ae0539496
89b763882f2d082d2e593f0141d0282a87ed808e blob 20 33 1291 1 773c00f165a12de64455024482c7e26ae0539496
7373cd3c4c9c0ce5ae2425df5dd173393088c680 blob 21 32 1371 2 89b763882f2d082d2e593f0141d0282a87ed808e| Column | Meaning |
|---|---|
| 1 | The object's ID |
| 2 | The type |
| 3 | The size of the object itself, in bytes |
| 4 | The size it takes in the pack, compressed |
| 5 | Its position (offset) in the .pack file |
| 6 and 7 | For a delta: how deep its chain of deltas is, and the ID of the object it is based on |
Explanation: one blob is stored in full: about 4 KB of text, compressed to a few hundred bytes. The other three have extra columns: they are deltas, each only a few bytes in the pack, saying "take that other blob and change one line". Two are based on the full blob directly (chain length 1). The oldest version, 7373cd3, is based on one of those deltas (chain length 2), so Git applies two deltas to rebuild it. Git usually keeps the newest version in full and stores older ones as deltas, because the newest is read most often. The exact compressed sizes may differ slightly on your computer, depending on its compression library.
git verify-pack -v .git/objects/pack/*.idx | grep -E "^(non delta|chain length)"non delta: 9 objects
chain length = 1: 2 objects
chain length = 2: 1 objectThe summary at the end counts how many objects are stored in full (the 9 includes the commits and trees), and how many are deltas at each chain length: two deltas one step from a full object, and one delta two steps away.
When Does Packing Happen?
| Moment | What happens |
|---|---|
git gc | Packs loose objects, packs refs, removes old reflog entries and deletes unreachable objects older than two weeks |
git gc --auto | Does the same only when needed. Many commands (commit, merge, fetch) run it automatically |
git clone, git fetch, git push | Objects travel over the network as a pack, and are usually kept as one |
git repack | Packs on demand, with more options (-a everything into one pack, -d remove redundant packs) |
git maintenance | Runs these jobs on a schedule in the background (Git 2.31 and later) |
git gc --auto decides by thresholds: by default when there are more than about 6,700 loose objects (gc.auto), or more than 50 packs (gc.autoPackLimit). Large repositories often turn on background maintenance instead, so that no command has to pause for it:
git maintenance startThis registers the repository with the system's scheduler (cron, launchd or Windows Task Scheduler), which then runs light jobs every hour, such as prefetching from remotes and packing loose objects, and heavier ones daily or weekly.
What git gc Deletes
git gc also removes objects that nothing reaches anymore: no branch, tag, reflog entry, stash or index entry. It only deletes them once they are older than gc.pruneExpire (two weeks by default). Combined with reflog expiry (30 or 90 days, see "Git Recovery"), this gives you weeks to recover lost commits. git gc --prune=now removes them immediately, which is almost never needed and destroys any chance of recovery.
Common Mistakes
Running git gc often "to be safe". Git runs gc --auto itself. Manual runs are rarely needed, and --aggressive is only worth it after big history rewrites.
Deleting files in .git/objects to save space. That corrupts the repository. Use git gc, or git filter-repo to remove large files from history.
Expecting a deleted large file to shrink the repository. It stays in history, and in every pack, until history is rewritten.
Measuring size with a file browser. git count-objects -vH reports loose and packed sizes in readable units.
Interrupting a repack. Git writes new packs before deleting old ones, so an interruption is safe, but it may leave a temporary file to clean up next time.
Under the Hood
A .pack file starts with a header (the text PACK, a version and the number of objects), followed by the objects, each with a small header (type and size) and its zlib-compressed data. A delta object holds the ID or offset of its base object, then a list of instructions: "copy bytes from the base at this position" or "insert these new bytes". Git chooses bases by sorting objects by type, file name and size, and trying to make deltas against neighbors within a window (pack.window, 10 by default), limiting chains to pack.depth (50) so that rebuilding an object stays fast. Deltas can be made between any two objects of the same type, even different files, which is one reason packs compress so well.
Interview Corner
What is a packfile?
A file in .git/objects/pack/ that stores many objects together, with an .idx index. Similar objects are stored as deltas against a base object, which saves a lot of space compared with one compressed file per object.
What is the difference between loose and packed objects?
A loose object is one zlib-compressed file under .git/objects/xx/. Packed objects live in a packfile, possibly as deltas. New objects start loose, and git gc or transfers move them into packs.
What does git gc do?
It packs loose objects and refs, removes redundant packs, expires old reflog entries and deletes unreachable objects older than the prune period (two weeks by default). Git runs it automatically with --auto when thresholds are reached.
Does Git store diffs or snapshots?
Logically, snapshots: every commit points to a complete tree. Physically, packfiles store many objects as deltas against similar objects to save space. The two levels are independent.
What is git maintenance?
A command (Git 2.31+) that runs repository maintenance tasks, such as prefetching, packing and updating the commit-graph, on a schedule in the background, so that commands don't pause for garbage collection.
Practice
Challenge: loose again. After the git gc above, Navika corrects one more fee and commits. How many loose objects and packed objects are there now?
What does Git print? Before you open the solution, decide which objects the new commit writes, and whether they go into the pack.
Show solution
sed -i.bak 's/^Student 4: .*/Student 4: $12,500/' fees.txt
rm fees.txt.bak
git commit --quiet -am "Correct the fee of student 4"
git count-objects -v | grep -E "^(count|in-pack|packs):"count: 3
in-pack: 12
packs: 1Three new loose objects (a blob, a tree and a commit), and the pack still holds the 12 older ones. New objects are always written loose first. The next git gc will pack them too.
Checkpoint. In a large repository you have (or a public one you clone), run git count-objects -vH before and after git gc, and look at the biggest blobs with git verify-pack -v .git/objects/pack/*.idx | sort -k3 -n | tail -5.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git count-objects -vH | Count loose and packed objects, with sizes |
git gc | Pack objects and clean up |
git verify-pack -v <pack>.idx | List a pack's objects and deltas |
git repack -a -d | Repack everything into one pack |
git maintenance start | Schedule background maintenance |
Was this tutorial helpful?