Large files and Git LFS
Why big binary files bloat a repository forever, how Git LFS swaps them for pointer files using clean and smudge filters, and what I could and could not run.
Packfiles, garbage collection and repo sizeConfiguration, aliases and attributes
Large files and Git LFS
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Binary files such as images, video and design files don't delta-compress well, and every version stays in history, so each change adds close to the full file size to every clone.
- Git LFS stores a small pointer file in the repository (a spec version, an `oid sha256:` and a `size`) and keeps the real bytes on a separate LFS server.
- Paths are chosen with `git lfs track` or a `.gitattributes` line with `filter=lfs diff=lfs merge=lfs -text`, and the pointer replaces the file at commit time through a clean filter.
- A smudge filter turns the pointer back into the real file at checkout, so you work with normal files in your folder.
- Track large files with LFS before committing them, because files already in history stay there unless you rewrite history (for example with `git lfs migrate import`).
Why large files hurt
The previous packfile lesson showed that Git stores text edits as tiny deltas, so eight versions of a 687 KB notes file cost about 111 KB. Binary files are different. A design file, a compiled asset or a video is already compressed, so a small edit changes most of its bytes. Git can’t find a small delta, and each version costs close to its full size.
Worse, history is permanent and clones download all of it. In the same lesson I committed a 3 MB random file and deleted it in the next commit. The repository stayed 3.1 MB. A 100 MB asset updated twenty times is 2 GB that every new teammate and every CI run downloads.
What LFS does
Git LFS (Large File Storage) keeps the heavy bytes out of the repository. In their place, Git commits a tiny text pointer:
version https://git-lfs.github.com/spec/v1
oid sha256:9eb9249f147132412adff322aa5be393272ca0b1cbbb59a04fdf413b80740f6a
size 200000
The real file goes to a separate LFS store, usually offered by the same hosting service, and is downloaded only when a checkout needs that version. History carries pointers of roughly 130 bytes, and clones stay small.
What I could and could not run
Git LFS was not installed on the machine where I wrote this lesson, so I could not run git lfs itself. git lfs version answered 'lfs' is not a git command. So no real LFS output appears below. I can describe its commands from the documentation, but I haven’t seen them run, so test them in a scratch repository first.
What I could run is the mechanism underneath, because LFS is built on ordinary Git features, .gitattributes filters. I wrote a stand-in with two small scripts to prove the idea. It is not LFS and doesn’t talk to an LFS server. It stores bytes in a local folder, but it uses the same pointer format and the same Git hooks into staging and checkout.
The mechanism: clean and smudge filters
.gitattributes names a filter for matching paths. A filter has two halves:
- clean: runs when a file goes into the index. Content in, committed form out.
- smudge: runs when a file is checked out. Committed form in, working file out.
My clean.sh hashed the content, copied the bytes into a side store, and printed a pointer. My smudge.sh read the pointer’s oid and printed the stored bytes.
$ git config filter.demo.clean $PWD/../clean.sh
$ git config filter.demo.smudge $PWD/../smudge.sh
$ git config filter.demo.required true
$ echo '*.psd filter=demo -diff -merge' > .gitattributes
$ head -c 200000 /dev/urandom > logo.psd
$ git add . && git commit -qm "Add logo"
Then I looked at what Git stored versus what is on disk:
$ git cat-file -s $(git rev-parse :logo.psd)
131
$ git cat-file -p :logo.psd
version https://git-lfs.github.com/spec/v1
oid sha256:9eb9249f...
size 200000
$ ls -l logo.psd | awk '{print $5}'
200000
The repository object is 131 bytes, a pointer, while my working file is the full 200,000 bytes. The bytes sit in the side store. Deleting and checking out again restores them:
$ rm logo.psd
$ git checkout -- logo.psd
$ ls -l logo.psd | awk '{print $5}'
200000
That is the whole trick. Real Git LFS replaces my two scripts with its own git-lfs program, which uploads to and downloads from an LFS server over HTTPS during push and checkout. The required true line matters. Without it, a machine lacking the filter program silently commits the raw file.
A related thing I saw directly: with the real .gitattributes line (filter=lfs ...) in place but git-lfs not installed, git add didn’t complain, and the 200,000-byte file went into history unchanged. Git quietly skips a filter it can’t find unless it is required. That is the likely way big files leak into a repository when a teammate hasn’t installed LFS.
The commands you’d use (from the documentation, not run)
git lfs install # once per machine: sets up the filters
git lfs track "*.psd" # appends the line to .gitattributes
git add .gitattributes logo.psd
git commit -m "Track PSDs with LFS"
git lfs ls-files # which files are stored as LFS
git lfs migrate import --include="*.psd" --everything # rewrite history
Commit .gitattributes first, then the files, so the filter applies. migrate import rewrites history, so it needs the same care as removing secrets from history: a force push and everyone re-cloning.
Costs and alternatives
LFS isn’t free. Hosting services cap LFS storage and bandwidth, and offer paid packs (check their current limits), clones need the LFS client, and offline work only has the versions you downloaded. Alternatives include keeping assets in object storage and referencing them by URL, using a package registry, or, for repositories that are merely big, partial clone, which doesn’t change what is committed but changes what you download.
What the video covers
GitKraken’s “Git Tutorial #37: How to Use Git LFS for Large File Storage” is about eight minutes and was published around 2022. From its title it shows setting up LFS, and as a GUI vendor it may use GitKraken’s client alongside the command line. I judge English from the title, description and channel only, and I could not read a transcript. Since I couldn’t run LFS myself, I can’t verify its commands against current versions. CodeLucky’s shorter explainer is linked as a runner-up.
Where this fits
LFS shrinks what history stores. Partial clone and sparse checkout shrink what you download and check out: partial clones and sparse checkout.
Check yourself
Why does committing a 50 MB binary ten times cost more than committing a 50 MB text file ten times?
Text edits delta-compress to tiny differences, but compressed binary data changes shift most bytes, so each version is stored nearly in full and every clone downloads them all.
What does a Git LFS pointer file contain?
Three lines: a `version` URL for the spec, `oid sha256:<hash>` of the real content, and its `size` in bytes.
What do the clean and smudge filters do?
Clean runs when files are staged and replaces the content with a pointer. Smudge runs on checkout and replaces the pointer with the real content.
You already committed a large file last month. Does `git lfs track` fix the history?
No. It only affects future commits. Existing history needs rewriting, such as with `git lfs migrate import`, followed by a force push.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.