Large and binary files
Last updated
Joaquin films Lola Remy making leche flan: a 200 MB video. He commits it to Kusina, realises, and deletes it in the next commit. The repository is still 200 MB bigger, and always will be. In this lesson you'll see why Git struggles with large and binary files, what to keep out, and what to use instead.
By the end of this lesson you can
- Explain why deleting a committed file doesn't make the repository smaller
- Say which files don't belong in a Git repository
- Explain what Git LFS does, and when to use it
- Use shallow and partial clones for large repositories
Why Git struggles
Git is built for text. Two things make large binary files (videos, photos, zip files, compiled programs, big datasets) a poor fit:
- No useful diffs. Git can't show what changed inside a video, and can't merge two edits of one. A conflict means picking one whole file.
- Every version is kept, in every clone. Change a 200 MB file three times and the history holds three of them, about 600 MB. Everyone who clones downloads all of it, forever.
Git does compress its storage, and a few small images are no problem. It's big files, or files that change often, that hurt.
Deleted, but still there
Joaquin's video, in three commits:
| Commit | What it did | Files you see | Repository size |
|---|---|---|---|
| Before | Recipes only | 0.1 MB | 0.3 MB |
| “Add leche flan video” | Committed flan.mp4 | 200 MB | 200.3 MB |
| “Remove the video” | Deleted flan.mp4 | 0.1 MB | 200.3 MB |
| Ten more commits later | Recipes only | 0.1 MB | 200.4 MB |
Deleting the file makes a new commit without it, but the commit with it is still in history: anyone can git switch --detach to it and get the video back. That's lesson 2.5's promise working as designed. The only way to truly remove it is rewriting history (lesson 7.6's tools), which, once pushed, breaks the golden rule for everyone.
git count-objects -vH count: 0 size: 0 bytes in-pack: 214 packs: 1 size-pack: 200.31 MiB prune-packable: 0 garbage: 0 size-garbage: 0 bytes
Hosts limit file sizes too: GitHub warns over 50 MB and refuses files over 100 MB.
What to keep out
Keep these out of Git, with .gitignore (lesson 1.6) and habit:
- Generated files: built programs,
index.htmlfrom the script,__pycache__. Anyone can make them again from the source. - Dependencies you download:
node_modules/, virtual environments. A file listing them is committed instead. - Media and datasets: videos, large photo sets, data dumps. Store them where they belong (a photo site, cloud storage, a data repository) and link to them.
- Secrets: passwords, keys,
.envfiles. Lesson 7.6.
Git LFS
When a project really needs large files versioned with its code (game art, design files, trained models), Git LFS (Large File Storage) helps. It keeps the large files on a separate server, and commits only a small pointer file in their place:
git lfs install git lfs track "*.mp4" Tracking "*.mp4" git add .gitattributes flan.mp4 git commit -m "Add leche flan video with LFS"
git lfs track writes the rule into .gitattributes, which you commit. From then on, matching files are stored by LFS; a clone downloads only the versions it checks out, not all of history. Most hosts support LFS, with storage limits on free accounts. Set it up before committing the files: moving files already in history needs a rewrite (git lfs migrate).
Big repositories: shallow and partial clones
For a huge repository you only want to build or read:
git clone --depth 1 <address>makes a shallow clone: only the newest commit, not the history. Fast, butgit logandgit blamecan't see the past.git clone --filter=blob:none <address>makes a partial clone: all the commits, but file contents downloaded only when needed. Good for working in a big project.
Neither shrinks the repository on the host; they just download less of it.
Spot the trap: commit, delete, expect it gone
Joaquin commits the 200 MB video, pushes, realises, deletes it, and pushes again. “Fixed it.”
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Git LFS: what it is and how to start
- GitHub Docs: About large files on GitHub: file size limits, and removing a large file from history
- Git documentation: git-clone, --depth and --filter: shallow and partial clones
- Pro Git: Git Objects: every version of every file is kept as an object