Git Large File Storage (LFS)
Git Large File Storage (LFS) is an extension to Git that stores large files, such as images, videos, design files and data sets, on a separate server, and keeps only a small text pointer to each one in the repository. It is used to stop large binary files from making every clone slow. With LFS, a clone downloads only the versions of large files that you check out, instead of every version ever committed.
Picture a museum catalog. Instead of keeping each painting in the catalog, it keeps a card that says which storeroom and shelf the painting is on. The catalog stays thin, and you fetch only the paintings you want to see.
Why Large Files Are a Problem in Git
Git keeps every version of every file forever, and every clone gets all of them. For text files this is cheap, because Git compresses them and stores small differences. A binary file, such as a scanned PDF or an image, barely compresses and changes completely each time, so ten versions of a 50 MB file cost about 500 MB in every clone, even if you only ever need the newest one.
The Fee Tracker team committed a scanned receipt book three times:
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
mkdir scans
for version in 1 2 3; do
head -c $((version * 300000)) /dev/zero | tr "\0" "$version" > scans/receipts.bin
git add scans
git commit --quiet -m "Update the receipt scan ($version)"
done
git rev-list --objects --all | git cat-file --batch-check="%(objecttype) %(objectsize) %(rest)" | grep "^blob" | sort -k2 -n -rblob 900000 scans/receipts.bin
blob 600000 scans/receipts.bin
blob 300000 scans/receipts.bin
blob 20 fees.txtExplanation: git rev-list --objects --all lists every object in the history with its path, and git cat-file --batch-check adds the type and size in bytes. The three versions of receipts.bin together are 1.8 MB, and every clone carries all three, while fees.txt is 20 bytes. This pair of commands is the standard way to find what makes a repository large.
Installing Git LFS
Git LFS is a separate program. Install it once, then run git lfs install once per computer:
# Git for Windows already includes Git LFS
git lfs installbrew install git-lfs
git lfs installsudo apt install git-lfs
git lfs installgit lfs install adds the LFS filter settings to your ~/.gitconfig and installs Git hooks in the current repository. Because Git LFS isn't installed on the computer where this page's output is checked, the git lfs commands below are shown without output.
Syntax
git lfs <command> [options]| Command | Description |
|---|---|
git lfs install | It is used to set up Git LFS for your user account (once per computer). |
git lfs track "<pattern>" | It is used to store files matching the pattern with LFS, by adding a line to .gitattributes. |
git lfs untrack "<pattern>" | It is used to stop tracking a pattern. |
git lfs ls-files | It is used to list the files in the current commit that are stored with LFS. |
git lfs pull | It is used to download the LFS files for the current commit. |
git lfs fetch --all | It is used to download every version of every LFS file (for a full backup). |
git lfs migrate import --include="<pattern>" | It is used to rewrite history so that existing files matching the pattern move into LFS. |
git lfs lock <file> / unlock | It is used to lock a file so that nobody else edits it at the same time. |
Example 1: Tracking a File Type
Navika tells LFS to handle every .bin and .pdf file in the project:
git lfs track "*.bin"
git lfs track "*.pdf"
git add .gitattributes
git commit -m "Store scans with Git LFS"Explanation: each git lfs track prints Tracking "<pattern>" and adds a line to .gitattributes, the file described in "Git .gitattributes and Line Endings". Commit it, so that everyone who clones the project uses LFS for these files. From now on, git add scans/receipts.bin stores the file with LFS.
The lines LFS writes are ordinary attributes, and plain Git can read them. Here is the line for *.bin, written by hand to show what Git sees:
echo "*.bin filter=lfs diff=lfs merge=lfs -text" > .gitattributes
git check-attr filter diff merge text -- scans/receipts.binscans/receipts.bin: filter: lfs
scans/receipts.bin: diff: lfs
scans/receipts.bin: merge: lfs
scans/receipts.bin: text: unsetExplanation: filter=lfs makes Git pass the file through the LFS program on its way in and out, diff=lfs and merge=lfs tell Git not to try showing or merging its contents line by line, and -text stops line-ending conversion. Without Git LFS installed, the filter=lfs line does nothing, which is why everyone on the team needs to install it.
Example 2: What Is Stored in the Repository
After git add and git commit, the repository holds a small text pointer in place of the file. git show HEAD:scans/receipts.bin prints the pointer. For the newest receipt scan, it would be:
version https://git-lfs.github.com/spec/v1
oid sha256:a1755974c2b7cff617cc7198509e18bac78e8b7b3d60894ba9eeb6f83e4cbcf9
size 900000Explanation: the pointer has three lines: the LFS format version, the SHA-256 hash of the real content (the oid) and its size in bytes. The real file is kept in .git/lfs/objects/, and on git push, an LFS hook uploads it to the LFS server (GitHub, GitLab and Bitbucket each provide one). In your working folder you always see the real file, never the pointer. git lfs ls-files lists the files stored this way.
Example 3: Moving Existing Files into LFS
git lfs track affects only files added from now on. The three old versions of receipts.bin are still in history. To move them into LFS, rewrite the history, on a fresh clone:
git lfs migrate info
git lfs migrate import --include="*.bin" --everything
git push --force-with-lease --allExplanation: migrate info reports which file types take the most space. migrate import rewrites every commit on every branch (--everything), replacing matching files with pointers and writing .gitattributes into each commit. Every commit gets a new ID, so this has the same consequences as any history rewrite (see "Removing Files from History"): agree with the team first, force-push, and ask everyone to clone again.
When to Use LFS
| Use LFS for | Don't use LFS for |
|---|---|
| Images, audio, video, design files (PSD, Figma exports) | Source code and other text files |
| Large data sets that the code needs | Build output (JAR, WAR, target/): don't commit it at all |
| Binary documents that change from time to time | Dependencies: use Maven, Gradle or npm |
| Any file over a few megabytes that you must version | Secrets: never commit them (see "Keeping Secrets Out of Git") |
Common Mistakes
Running git lfs track after committing the files. Tracking only applies to new additions. Use git lfs migrate import for existing history.
Forgetting to commit .gitattributes. Teammates' Git then adds the files without LFS.
A teammate without Git LFS installed. They see pointer files instead of the real content. Everyone needs to install it.
Expecting LFS to shrink a repository by itself. Old versions stay in history until you migrate them.
Using LFS as a backup. A normal clone downloads only the current versions. Use git lfs fetch --all to download every version.
Under the Hood
LFS works through Git's clean and smudge filters, described in "Git .gitattributes and Line Endings". When you git add a tracked file, Git runs the LFS clean filter, which copies the content into .git/lfs/objects/ (named by its SHA-256 hash) and gives Git the three-line pointer to store as the blob. When Git checks out a file, the smudge filter reads the pointer and writes the real content, downloading it from the LFS server first if necessary. A pre-push hook uploads new LFS objects before Git pushes the commits, using a separate LFS API over HTTPS. Setting GIT_LFS_SKIP_SMUDGE=1 before a clone leaves the pointers in place, and git lfs pull fetches the real files later.
Interview Corner
What is Git LFS and why is it used?
Git LFS stores large files on a separate server and keeps small text pointers in the repository. It keeps clones fast, because a clone downloads only the large files of the checked-out commit instead of every version.
How do you start storing a file type with LFS?
Run git lfs install once, then git lfs track "*.psd" and commit the changed .gitattributes.
Large files are already in history. How do you move them to LFS?
git lfs migrate import --include="*.psd" --everything, then force-push. It rewrites history, so the team must agree and clone again.
How do you find the largest files in a repository's history?
git rev-list --objects --all | git cat-file --batch-check="%(objecttype) %(objectsize) %(rest)", then sort by size. git lfs migrate info and git filter-repo --analyze give similar reports.
Practice
Challenge: the largest version. Print only the single largest blob in Navika's history, with its size and path.
What does Git print? Before you open the solution, decide which file and which size it shows.
Show solution
git rev-list --objects --all | git cat-file --batch-check="%(objecttype) %(objectsize) %(rest)" | grep "^blob" | sort -k2 -n -r | head -1blob 900000 scans/receipts.binThe third version of scans/receipts.bin, 900000 bytes. The same command on a real repository shows at once which files are worth moving to LFS.
Checkpoint. Install Git LFS, create a practice repository, track *.png, commit an image, and run git lfs ls-files and git show HEAD:<image path> to see the pointer.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git lfs install | Set up LFS once per computer |
git lfs track "<pattern>" | Store a file type with LFS |
git lfs ls-files | List LFS files |
git lfs pull | Download LFS files for the current commit |
git lfs migrate import --include="<pattern>" --everything | Move existing files into LFS |
Was this tutorial helpful?