Sub topic 1
Why Large Binary Files Are a Problem for Git
Prerequisites
This chapter assumes you can clone, add, commit, push, and pull with Git. You should know what branches and remotes are, and be comfortable running commands in a terminal. You should also know that .gitignore and .gitattributes files exist. You do not need any LFS experience. Later chapters cover the tool itself.
How Git stores history
Git is a distributed version control system. Every clone contains the full history of the repository, not only the latest files. That design makes most operations fast and lets you work offline. It also means that every version of every file you ever committed is copied to every developer's machine.
For source code, this cost is small. Text files are compact, and Git compresses them well. It also stores many versions of a text file efficiently, because it can pack similar objects together and keep only the differences between them.
Why binaries behave differently
Large binary files, such as design files, datasets, videos, and game assets, break these assumptions in three ways.
- They are big. A single 50 MB file is already larger than the source of many whole projects.
- They compress poorly. Formats like PNG, MP4, and ZIP are already compressed, so Git cannot shrink them further.
- They delta poorly. A small edit in a Photoshop file can change much of its bytes. Git's delta compression then saves little, so each version costs close to its full size.
The result is that every revision adds roughly its full size to the repository, and that history can never be removed without rewriting it.
A worked example
Suppose a design team commits design/logo.psd at 50 MB and revises it 20 times. Each revision is mostly different bytes, so the history holds about 1 GB for that one file. A new teammate who clones the repository downloads all 1 GB, even though they need only the latest 50 MB version.
You can see this cost yourself. Run these commands in any repository you have cloned:
git count-objects -vH
git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | sort -k3 -n -r | head -5The first command prints the size of your object database. The second lists the five largest objects across all history, with their sizes in bytes and their paths. If a binary file appears near the top, it is a candidate for LFS.
Symptoms you will notice
- Cloning and fetching become slow, and they get slower as history grows.
- Disk usage in
.gitgrows with every revision of a binary file. - Some hosting providers reject pushes of files above a size limit. GitHub, for example, blocks files larger than 100 MiB.
- Operations that scan history, such as
git gc, take longer.
Why deleting the file does not help
Removing a large file in a new commit only hides it from the latest snapshot. The old commits still reference it, so it stays in the history and in every clone. Shrinking the repository requires rewriting history, which changes commit IDs and forces everyone to resynchronize. It is much better to avoid committing large binaries to regular Git in the first place.
What this means for the rest of the tutorial
Git LFS addresses this problem by keeping the large content outside the normal history and storing only small placeholders in Git. The next chapter explains how those placeholders, called pointer files, work.
Exercise
A teammate commits a 200 MB video, notices the mistake, and removes it in the next commit. Why is the repository still large, and what is the only way to shrink it?
Show answer
The first commit still references the video, and Git keeps every committed object in history, so every clone still downloads it. The only way to shrink the repository is to rewrite history so the file is removed from earlier commits. That changes commit IDs and requires a coordinated force push.
Exercise
Why does a small edit to a compressed binary format like a PSD or MP4 add close to the file's full size to the repository?
Show answer
Such formats are already compressed, so a small edit can change many bytes. Git's delta compression finds few similarities between versions, so each revision is stored at close to its full size.