Large Files: Git LFS and Keeping Binaries Out of History
Why large binary files make repositories slow and why deleting them doesn't help, finding the biggest files in history, storing them with Git LFS (install, track, pointer files, locking, migrate, fetch settings), hosting limits, and alternatives such as artifact storage.
kvi5hnuCode Sprout team member7 min readOn this page
Git is designed for text: source code that changes a few lines at a time, compresses well, and diffs and merges meaningfully. Large binary files, such as videos, design files, datasets, game assets and compiled builds, work against that. Every version is stored in full, forever, and every clone downloads all of them. This article explains the problem, how to find large files already in a repository, and Git LFS (Large File Storage), the standard solution.
Why big binaries hurt
git init -q trail-guide && cd trail-guide
for v in 1 2 3 4 5; do
head -c 1000000 /dev/urandom > trail-video.bin # incompressible, like real media
git add trail-video.bin; git commit -q -m "Video version $v"
done
git gc -q
echo "after five versions: $(git count-objects -vH | grep size-pack)"
git rm -q trail-video.bin; git commit -q -m "Remove the video"
git gc -q
echo "after deleting it: $(git count-objects -vH | grep size-pack)"
after five versions: size-pack: 4.77 MiB
after deleting it: size-pack: 4.77 MiB
- Compressed media barely shrinks, and versions don't share content, so five 1 MB versions need about 5 MB.
- Deleting the file doesn't reduce the size: every old version is still in history, and every clone still downloads it. Only rewriting history removes it (article 47).
- Binary files can't be diffed or merged as text, so two people editing the same file always conflict.
Hosting services set limits too: GitHub, for example, warns about files over 50 MB and rejects files over 100 MB in normal Git pushes.
Finding the largest files in history
To see what's making a repository big, list the largest blobs anywhere in its history, with their paths:
git init -q trail-guide && cd trail-guide
echo "Lake loop" > lake.txt
mkdir photos; head -c 300000 /dev/urandom > photos/lake.jpg
head -c 2000000 /dev/urandom > backup.zip
git add .; git commit -q -m "Add files"
git rm -q backup.zip; git commit -q -m "Remove backup"
git rev-list --objects --all |
git cat-file --batch-check='%(objecttype) %(objectsize) %(rest)' |
awk '$1 == "blob" {print $2, $3}' | sort -rn | head -3
2000000 backup.zip
300000 photos/lake.jpg
10 lake.txt
rev-list --objects --all lists every object reachable from any reference, with the path it was first seen at, and cat-file --batch-check adds type and size. The deleted backup.zip is still the biggest thing in the repository. For a fuller report, git filter-repo --analyze writes size statistics per path and extension (article 47), and git repo structure summarizes object counts and the largest objects (article 32).
Git LFS
Git LFS is an extension, maintained by GitHub and supported by GitHub, GitLab, Bitbucket, Azure DevOps and most self-hosted servers. It stores large files on a separate LFS server, and keeps only a small pointer file in the repository. Clones download the actual content for the commit you check out, not for all of history.
Setting it up
git lfs install # once per machine: installs Git's hooks and filter settings
git lfs track "*.psd" "*.mp4" # in each repository: writes rules to .gitattributes
git add .gitattributes
git add design/cover.psd
git commit -m "Add cover design with LFS"
git push # sends commits to Git, and file contents to the LFS server
git lfs install is needed once per user. Git LFS is bundled with Git for Windows, and available as a package elsewhere (brew install git-lfs, apt install git-lfs). git lfs track adds lines like this to .gitattributes (article 35):
*.psd filter=lfs diff=lfs merge=lfs -text
*.mp4 filter=lfs diff=lfs merge=lfs -text
The filter=lfs attribute is what does the work: when a matching file is staged, the LFS clean filter uploads its content to a local LFS store and stages a pointer instead. On checkout, the smudge filter replaces pointers with the real content. Commit .gitattributes before or with the files, because files added before tracking are stored in Git normally.
What the repository stores
The pointer that's committed in place of a large file looks like this:
git init -q trail-guide && cd trail-guide
head -c 5000000 /dev/zero > cover.psd # stand-in for a design file
oid=$(sha256sum cover.psd | cut -d' ' -f1)
size=$(wc -c < cover.psd)
printf 'version https://git-lfs.github.com/spec/v1\noid sha256:%s\nsize %s\n' "$oid" "$size"
echo "cover.psd filter=lfs diff=lfs merge=lfs -text" > .gitattributes
echo "=== the attributes that apply"
git check-attr filter diff merge text -- cover.psd
version https://git-lfs.github.com/spec/v1
oid sha256:b39781589c4403fb82174c9647a010464cff38bad976547d339899b00053a545
size 5000000
=== the attributes that apply
cover.psd: filter: lfs
cover.psd: diff: lfs
cover.psd: merge: lfs
cover.psd: text: unset
The pointer records the LFS specification version, the SHA-256 hash of the content, and its size: about 130 bytes, however large the file is. With git-lfs installed, Git would commit exactly such a pointer for cover.psd. Because the sandbox doesn't include git-lfs, this example builds it by hand.
Daily use
| Command | Purpose |
|---|---|
git lfs ls-files | files in the current commit stored with LFS |
git lfs status | LFS files staged or changed |
git lfs pull | download missing LFS content for the current checkout |
git lfs fetch --all | download LFS content for all branches, such as for a backup |
git lfs prune | delete old LFS content from your local cache |
GIT_LFS_SKIP_SMUDGE=1 git clone <url> | clone with pointers only, downloading content later with git lfs pull |
git lfs lock <file> / unlock | lock a file on the server, so others can't push changes to it |
Locking is valuable for files that can't be merged: mark them lockable with git lfs track --lockable "*.psd", and LFS makes them read-only until you git lfs lock them.
Moving existing files into LFS
Files already committed normally stay in history until you rewrite it. git lfs migrate does that:
git lfs migrate info --everything # what would benefit
git lfs migrate import --include="*.psd,*.mp4" --everything # rewrite all branches to use LFS
git push --force-with-lease --all
Rewriting changes every commit ID from the first affected commit onwards, so coordinate with everyone first (article 47). Without --everything, migrate import only rewrites the current branch.
Costs and limits
- LFS storage and bandwidth are usually metered by hosting services, with a free allowance. CI pipelines that clone often can use a lot of bandwidth, so cache LFS content or skip it in jobs that don't need it.
- People without
git-lfsinstalled see pointer files instead of content, and might commit them back. Make LFS part of the project's setup instructions. - LFS doesn't make binaries mergeable: use locking for files that people edit.
Other options
Not every large file belongs in Git at all:
- Build outputs (compiled binaries, packages, Docker images) belong in an artifact repository or package registry, created by CI, not committed. Ignore them with
.gitignore(article 8). - Datasets and machine learning models can use tools such as DVC, or object storage, with only metadata in Git.
- Very large repositories with many files, rather than large files, benefit from partial clone and sparse checkout (article 44). A blob-less clone (
--filter=blob:none) already downloads old versions only on demand, which helps with moderately sized binaries even without LFS.
What's current
- Git LFS is supported by all major hosting services and bundled with Git for Windows. It needs
git lfs installonce per user. - Hosting services reject very large files in normal pushes, such as GitHub's 100 MB limit, and meter LFS storage and bandwidth.
- Partial clone with
--filter=blob:noneis an alternative for repositories with moderate binary history, without a separate LFS server.
Common questions
I committed a 200 MB file and the push was rejected. Removing it in a new commit didn't help. Why?
The file is still in a commit that's being pushed. If it's only in unpushed commits, remove it from them: amend the commit, or use interactive rebase (article 15). If it's deeper in history, use git filter-repo or git lfs migrate import to rewrite, then push.
After cloning, my images are tiny text files starting with version https://git-lfs.... What happened?
You have the LFS pointers but not the content, usually because git-lfs isn't installed or git lfs install hasn't been run. Install it, run git lfs install, then git lfs pull.
Does git lfs track convert files I already committed?
No. It only affects files staged after the rule exists. Use git lfs migrate import for files already in history.
Should I put everything binary in LFS? No. Small binaries that rarely change, such as icons, are fine in Git. Use LFS for large files, and files that change often. Build outputs shouldn't be committed at all.
Summary
- Every version of every committed file stays in history, and binaries don't compress or merge. Deleting a large file doesn't shrink the repository.
- Find large files with
git rev-list --objects --all | git cat-file --batch-checkorgit filter-repo --analyze. - Git LFS stores content on a separate server and commits small pointer files.
git lfs installonce,git lfs track "<pattern>", and commit.gitattributes. git lfs pull,ls-files,lockandmigrate importcover daily use and conversion.- LFS storage and bandwidth are limited and often metered. Build outputs belong in artifact storage, not Git.
Practice questions
Your designers commit large .psd files that are often edited. Set up the repository so these go to LFS and can't be edited by two people at once.
Answer
git lfs install
git lfs track --lockable "*.psd"
git add .gitattributes
git commit -m "Store PSD files with Git LFS, lockable"
Then designers run git lfs lock design/cover.psd before editing, and git lfs unlock after pushing. Files already committed need git lfs migrate import --include="*.psd" --everything to move them into LFS, which rewrites history.
A repository is 3 GB, though the current files are only 200 MB. How do you find out why?
Answer
Large objects are probably in history, even if deleted since. List the biggest blobs with:
git rev-list --objects --all |
git cat-file --batch-check='%(objecttype) %(objectsize) %(rest)' |
awk '$1 == "blob"' | sort -k2 -rn | head -20
or run git filter-repo --analyze. Then decide whether to rewrite history to remove them or move them to LFS (article 47).
Why doesn't committing .gitattributes with *.mp4 filter=lfs ... move existing videos into LFS?
Answer
Attributes only affect files when they're staged. Videos committed earlier are already stored as normal Git blobs in those commits, and history isn't changed. New and modified videos staged after the rule go to LFS. Converting the old ones requires rewriting history, with git lfs migrate import.
Previous: Subtrees: Including Another Project's History
Next: Large Repositories: Sparse Checkout, Partial Clone, Maintenance and Scalar

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.