Packfiles, garbage collection and repo size

Intermediate · 5 min

Watch loose objects become a delta-compressed packfile with git gc, find what makes a repo big, and see why deleting a large file doesn't shrink history.

Before thisObjects, blobs, trees and commitsRefs, branches and HEAD

CodeLucky · 5 min

Packfiles, garbage collection and repo size

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by CodeLuckyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • New objects are stored loose, one zlib-compressed file each, and `git gc` bundles them into a packfile where similar objects are stored as deltas against each other.
  • A packfile with its index makes a repository with many versions of a large file far smaller than the loose objects, because only the differences are kept.
  • `git count-objects -vH` shows loose versus packed counts and sizes, and `git verify-pack -v` shows each packed object's size and delta chain.
  • Git runs `git gc --auto` for you after enough loose objects pile up, and `git gc --prune=now` after `git reflog expire` removes unreachable objects immediately.
  • Deleting a large file in a new commit doesn't shrink the repository, because every old commit still contains it, and shrinking needs history rewriting.

Snapshots could be wasteful

The object model stores a full snapshot per commit. Change one line in a 700 KB file and, naively, you’d store another 700 KB blob. After a hundred small edits you’d have 70 MB for what is really 700 KB plus a hundred tiny changes. Git avoids this with packfiles.

I tested it on a jot repository with a 20,000-line notes.txt (about 687 KB) and eight small commits that each append a line. Git 2.54; sizes will differ a little on your machine.

Loose objects first

Every object starts life loose: a zlib-compressed file in .git/objects/. Each version of the big file is its own compressed copy:

$ git count-objects -vH
count: 27
size: 1.16 MiB
in-pack: 0
packs: 0
size-pack: 0 bytes
$ du -sh .git
1.3M

27 loose objects, 1.16 MiB, with nine near-identical blobs each costing about 110 KB after compression.

git gc packs and deltifies

$ git gc
$ git count-objects -vH
count: 0
size: 0 bytes
in-pack: 27
packs: 1
size-pack: 111.45 KiB
$ du -sh .git
232K
$ ls .git/objects/pack
pack-f7c9747c....idx
pack-f7c9747c....pack
pack-f7c9747c....rev

Twenty-seven objects in one pack, and 1.16 MiB became 111 KiB. The .pack holds the data, the .idx is a lookup table from hash to offset, and the .rev is a reverse index. How did eight copies of a 687 KB file fit? Ask the pack:

$ git verify-pack -v .git/objects/pack/pack-*.idx
615d95e7... blob   686757 110195 1317
...
non delta: 19 objects
chain length = 1: 8 objects

Columns are hash, type, size, size in the pack, and offset. One blob is stored whole (686,757 bytes, compressed to 110,195), and the other eight are deltas against it: instructions like “copy these bytes from the base, then add this line”. Eight objects have delta chain length 1. The biggest-object listing shows it from the outside:

$ git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(objectsize:disk) %(rest)' | sort -k3 -n -r | head -3
blob 615d95e... 686757 110195 notes.txt
blob 113cef8... 686749 42 notes.txt
blob d590e7e... 686741 42 notes.txt

Each version is about 687 KB logically, but costs 42 bytes on disk as a delta. Git chooses delta bases by similarity, not by commit order, which is why it works even when you didn’t commit in a tidy way. Commit-level diffs you see in git log -p are computed on demand, and aren’t what is stored.

When it runs

You rarely run git gc yourself. Commands like git commit, git merge and git fetch call git gc --auto, which does work only when thresholds are crossed (by default roughly 6,700 loose objects or 50 packs, according to the docs). git maintenance schedules background upkeep. Cloning and fetching also receive packs: the server builds one pack with exactly what you lack, which is why a clone is fast.

gc also removes unreachable objects, but only older than a grace period (two weeks by default), and only after reflog entries expire. That is the mechanism behind recovering lost work, and it is why I used git reflog expire --expire=now --all followed by git gc --prune=now in the earlier lesson to delete immediately.

Why a deleted big file stays

Now the surprise. I added a 3 MB random file, committed, deleted it, committed, and ran git gc:

$ head -c 3000000 /dev/urandom | base64 > dump.bin
$ git add . && git commit -qm "Add dump"
$ git rm -q dump.bin && git commit -qm "Remove dump"
$ git gc -q; du -sh .git
3.1M
$ ls
notes.txt
$ git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectsize:disk) %(rest)' | sort -k2 -n -r | head -2
blob 3029952 dump.bin
blob 110195 notes.txt

The working directory has no dump.bin, but .git grew from 232K to 3.1M. The “Add dump” commit is still in history and references the blob, so it can’t be collected, and random data doesn’t compress. Every clone downloads it forever. That is the same fact as the leaked secret in removing secrets from history: history is permanent unless you rewrite it. Shrinking means git filter-repo or filter-branch, then expire and gc. Prevention is better. Keep large binaries out of history, or put them under Git LFS.

A quick size checklist

  1. git count-objects -vH for the overall picture.
  2. The biggest-objects pipeline above to find what’s heavy.
  3. git gc if there are many loose objects; git gc --aggressive rarely helps and is slow.
  4. Rewrite history only if something large or secret should never have been committed.

What the video covers

CodeLucky’s “Git Internals: Understanding Packfiles for Efficient Repository Storage” is about five minutes and was published around 2025. From its title it covers how packfiles store objects efficiently. It is short, so the git gc walkthrough, verify-pack reading and the deleted-large-file section come from my own runs. I judge English from the title, description and channel only, and I could not read a transcript. CodeLucky also made this group’s objects and refs videos, so the three follow on. Two longer packfile lessons are linked.

Where this fits

That finishes what Git stores. Next, how to hook into what Git does: Git hooks.

Check yourself

  1. 01What is a loose object, and what is a packfile?

    A loose object is one compressed file under `.git/objects/xx/`. A packfile bundles many objects into one file, storing similar ones as deltas, with an index for lookup.

  2. 02What does `git gc` do besides packing?

    It also packs refs, removes unreachable objects older than the grace period, and prunes expired reflog entries.

  3. 03You committed a 3 MB file and then deleted it in the next commit. Is the repository smaller?

    No. The earlier commit still references the file's blob, so the objects stay. Only rewriting history, then expiring and collecting, removes them.

  4. 04How can you list the biggest objects in a repository?

    Pipe `git rev-list --objects --all` into `git cat-file --batch-check` with a size format, then sort by size.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark