TExcelTAD
Home › Git › Git Large File Storage (LFS)

Git Large File Storage (LFS)

Git stores every version of every file forever, and every clone downloads all of it. That is perfect for source code, but painful for large binary files (videos, game assets, datasets, design files): a 200 MB file changed ten times adds about 2 GB to every clone. Git LFS is an extension that keeps such files outside the normal history.

The problem, measured

Each committed version of a file is a separate blob object. Here an 80 KB "dataset" is changed three times; the repository holds all three full versions:

bash · try it yourself
mkdir research && cd research && git init -q
for run in 1 2 3; do
  seq -f "sample $run, value %g" 1 4000 > data.csv     # about 80 KB
  git add data.csv && git commit -q -m "Dataset run $run"
done
git rev-list --objects --all |
  git cat-file --batch-check="%(objecttype) %(objectname) %(objectsize) %(rest)" |
  grep "^blob" | cut -c1-12,46-
blob 4ddba01 82893 data.csv
blob dbb07d9 82893 data.csv
blob 0fe9432 82893 data.csv

Text compresses well, so Git copes with this. Binary formats (video, images, zip files) barely compress, can't be delta-compressed well and can't be merged, so they grow a repository quickly. GitHub also blocks files over 100 MB.

How LFS works

With LFS, the repository stores a tiny pointer file instead of the content, and the real file goes to a separate LFS server (GitHub, GitLab and Bitbucket all provide one):

version https://git-lfs.github.com/spec/v1
oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393
size 12345
Step Happens
git add video.mp4 a clean filter moves the content into .git/lfs/objects and stages the pointer
git push the content is uploaded to the LFS server, the pointer is pushed as normal
git clone / git checkout a smudge filter downloads the content for the commit you check out (only that version)

Setting it up

bash
# once per computer (after installing git-lfs from git-lfs.com or your package manager)
git lfs install

# per repository: choose what LFS manages; this writes .gitattributes
git lfs track "*.psd" "*.mp4" "assets/models/**"
git add .gitattributes
git add design.psd
git commit -m "Add design file via LFS"
git push

git lfs ls-files          # which files are in LFS
git lfs migrate import --include="*.psd"   # move existing history into LFS

git lfs track simply adds lines like these to .gitattributes:

bash · try it yourself
mkdir game && cd game && git init -q
cat > .gitattributes <<'EOF'
*.psd filter=lfs diff=lfs merge=lfs -text
*.mp4 filter=lfs diff=lfs merge=lfs -text
EOF
touch cover.psd intro.mp4 README.md
git check-attr filter diff merge text -- cover.psd README.md
cover.psd: filter: lfs
cover.psd: diff: lfs
cover.psd: merge: lfs
cover.psd: text: unset
README.md: filter: unspecified
README.md: diff: unspecified
README.md: merge: unspecified
README.md: text: unspecified

-text turns off line-ending conversion; filter=lfs hooks in the clean and smudge filters that the git lfs install step registers in your Git config.

What a pointer contains

A pointer is just the SHA-256 of the content and its size. You can compute the same values yourself:

bash · try it yourself
mkdir demo && cd demo
head -c 5000 /dev/zero | tr '\0' 'A' > texture.bin
printf 'version https://git-lfs.github.com/spec/v1\noid sha256:%s\nsize %s\n' \
  "$(sha256sum texture.bin | cut -d' ' -f1)" "$(wc -c < texture.bin)"
version https://git-lfs.github.com/spec/v1
oid sha256:260679791fa8da4dddc6aa3b243c514025e83d3a2f60800b9734b990be5d11a0
size 5000

That pointer, about 130 bytes, is what Git commits, no matter how big the file is.

Things to know

  • Everyone who works on the repository needs git lfs installed; without it they get the pointer text instead of the file.
  • Hosting services meter LFS storage and bandwidth separately from Git.
  • Decide on LFS early: files already committed normally stay in history until you rewrite it with git lfs migrate.
  • Locking (git lfs lock design.psd) helps with files that can't be merged.

Test yourself

What does a repository using LFS store in its Git history for a large file?

  1. The whole file, compressed
  2. A small pointer file with the content's hash and size
  3. Nothing
  4. A link to Dropbox

Which file does git lfs track "*.mp4" modify?

  1. .gitignore
  2. .gitattributes
  3. .git/config only
  4. README.md

Why are large binary files a problem in plain Git?

  1. Git can't store binary files
  2. Every version is kept forever and downloaded by every clone, and binaries compress and merge poorly
  3. They are automatically ignored
  4. They break branches