Sub topic 1
Why large binary files are a problem for Git
Git works well for source code, and it can store anything you commit. Large binary files such as design sources, datasets, videos, and game assets still strain it. This chapter explains the mechanisms behind that strain. The rest of the tutorial builds on them.
Prerequisites
This chapter assumes you can:
- Run
git clone,add,commit,push, andpullfrom a terminal. - Work with branches and remotes.
- Read a short shell script. The measurements below use
bash,head,awk, andsort.
You don't need to know Git internals. The chapter introduces the parts you need.
What Git stores
Git is a content-addressed database. Every file version you commit becomes a blob, an object named by a hash of its contents. Trees record directory layouts, and commits point to trees. Git never edits an object in place. A changed file produces a new blob, and the old blob stays reachable through older commits.
This design has two consequences that matter for large files:
- History is permanent storage. The repository holds every version of every file that any reachable commit references.
- A normal clone copies all of it.
git clonedownloads the full history, so each developer pays for every version ever committed.
For text, this cost is small. A one-line change to a source file adds a tiny delta. A binary file behaves differently.
Why binaries cost more than text
Git saves space in two ways. It compresses each object with zlib. When it builds a packfile, it also stores similar objects as deltas, meaning a base object plus a small set of differences. Both techniques work best on text. They work poorly on most binary formats:
| Property | Source code | Typical large binary |
|---|---|---|
| Compresses well with zlib | Yes, often 3x to 5x | Often not, because PNG, JPEG, MP4, ZIP, and many other formats are already compressed |
| Delta-compresses well | Yes, small edits give small deltas | Often not, because one edit can change most bytes in the file |
| Line-based diff is useful | Yes | No |
| Three-way merge is possible | Usually | Almost never |
A small edit in an uncompressed format may still produce a small delta. In a compressed or packed format, a small edit in the editor can rewrite the whole file. A 200 MB Photoshop file saved ten times can therefore cost close to 2 GB in history.
Delta compression also has a hard limit. Files larger than core.bigFileThreshold, which defaults to 512 MiB, are never delta-compressed. Git stores them whole in every version.
A measurement you can run
The script below creates a throwaway repository. It commits three versions of a 5 MB file filled with random bytes. Random bytes are a worst case for compression, and they stand in for already-compressed formats. It then deletes the file and checks the repository size.
git init size-demo
cd size-demo
git config user.name "Demo User"
git config user.email "[email protected]"
for i in 1 2 3; do
head -c 5000000 /dev/urandom > asset.bin
git add asset.bin
git commit -m "Version $i of asset.bin"
done
git count-objects -vH
git rm asset.bin
git commit -m "Remove asset.bin"
git count-objects -vHBoth count-objects reports show about 14 MiB of object data (the size line for loose objects). The exact figure varies slightly. Three 5 MB files are about 14.3 MiB, and the random data does not compress. Removing the file in the last commit changes nothing, because the earlier commits still reference all three blobs.
The working directory is empty, but the repository is still 14 MiB. Anyone who clones it downloads all three versions.
Finding what is heavy in a repository
When a repository feels too large, find the biggest objects in history. This pipeline lists every blob reachable from any ref, sorted by size:
git rev-list --objects --all \
| git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' \
| awk '$1 == "blob"' \
| sort -k3 -n -r \
| head -10git rev-list --objects --allprints every object reachable from every ref, with a path for blobs and trees.git cat-file --batch-checkadds the type and size to each line using the format string.awkkeeps only blobs.sort -k3 -n -rsorts by the third column (size) from largest to smallest.
The last column shows a path. The same file name may appear on several lines with different hashes. Those are the old versions that the repository still carries.
Where the problems show up
Clone and fetch time
The first clone downloads the whole history. Fetches get slower too, since a changed binary is a new full-size object. A new teammate or a fresh CI runner pays this cost every time.
Disk space
A clone stores the packed history in .git and a checked-out copy in the working tree. Binary history is stored once in .git and again for the files you check out. History for a file you deleted years ago still sits in .git.
Memory and CPU during pack operations
Commands such as git gc, git repack, and the pack-building step of git push read and compare large objects. Large blobs can raise memory use and slow these steps. Pushes of big objects can time out on slow links or behind proxies that cap request size.
Hosting limits
Hosting services set their own limits. GitHub, for example, warns for files above 50 MiB and rejects pushes that include a file above 100 MiB. It also recommends keeping repositories small. Other hosts have different numbers. A push failing at the limit is often the first sign that a repository needs a different approach.
Collaboration
Git merges text by comparing lines. It cannot merge two independent edits of a PSD, a Blender scene, or a compiled library. If two people change the same binary on different branches, one set of changes is lost, or someone picks a winner by hand. Chapter 10 of this tutorial covers locking as a way to prevent this.
Why deleting a file does not help
New users often try one of these fixes. None of them reduces repository size:
| Attempt | Why it fails |
|---|---|
| `git rm big.bin` and commit | The file leaves the latest tree, but older commits still reference the blob |
| Add the file to `.gitignore` | `.gitignore` only affects untracked files, not content already committed |
| `git gc` | Garbage collection removes only unreachable objects, and committed history keeps them reachable |
| `git clone --depth 1` | It reduces what you download, but not what the server stores, and it limits history operations |
The size is removed only when the objects stop being reachable. That requires rewriting history with a tool such as git filter-repo or git lfs migrate. A rewrite changes commit IDs, so every collaborator has to re-sync. Chapter 8 covers the migration case.
What a better design looks like
The problems above point to what a fix needs:
- Keep large file contents out of the object database that every clone downloads.
- Keep a small, text-like record in history so commits still identify an exact file version.
- Fetch the large content only when a user needs it, usually just the current version.
Git LFS does this. Git commits a small text pointer in place of the file, and the actual content lives on a separate server. The next chapter describes how pointers and the LFS object store work.
Common mistakes at this stage
- Assuming that a deleted file is gone. It stays in history until the history is rewritten.
- Judging size from the working tree. Use
git count-objects -vHor the blob listing above, not file explorer sizes. - Committing generated artifacts. Build outputs, installers, and exported renders are the usual cause of runaway size. Generate them in CI instead of committing them where you can.
- Treating a single large commit as harmless. One 80 MB asset is fine once. The same asset re-saved weekly adds up quickly.
Exercise
A teammate committed a 90 MB video, noticed it by accident, and pushed a new commit that runs git rm video.mp4. The repository is still 90 MB larger than before. Explain why, and name what would actually remove the space.
Show answer
The new commit removes the file only from the latest snapshot. Earlier commits still point to the video's blob, so it stays reachable and is still downloaded on clone. git gc cannot remove it because it is reachable. The space is freed only by rewriting history so no commit references the blob, for example with git filter-repo or git lfs migrate, followed by a coordinated force push.
Exercise
You run the three-version demo with a 5 MB random file and then repeat it with a 5 MB plain text file where each version changes one line. Which repository ends up smaller after git gc, and why?
Show answer
The text file repository ends up much smaller. Text compresses well with zlib, and after git gc Git can store the later versions as small deltas against the first. The random file neither compresses nor produces small deltas, so all three versions keep nearly full size.