Git LFS & .gitattributes
Git LFS (Large File Storage) keeps big binary files out of Git's history: the repository commits a small pointer file, and the content lives on an LFS server. The mechanics are explained in the Git course (Git LFS, .gitattributes) and a starter .gitattributes for Unity is in Git, Git LFS & .gitignore. This lesson is about running LFS on a real game team: choosing patterns, catching the files that slipped past LFS, and keeping clones and CI fast.
Patterns: what goes to LFS
A game's .gitattributes names every binary type the project uses. Rules worth knowing:
- Patterns are matched against the path, and on Linux (and therefore on most CI machines) case-sensitively:
*.pngdoes not matchHero.PNG. - A pattern without a slash matches the file name in any folder;
Art/**matches everything underArt/. - Later lines win when several set the same attribute, so specific exceptions go after general rules.
lockablemarks files that should be locked before editing (Locking Binary Files).
git check-attr shows exactly what Git decides for a path, which is the quickest way to debug a pattern. This session was run with real Git 2.54:
mkdir game && cd game && git init -q
cat > .gitattributes <<'EOF'
* text=auto
*.png filter=lfs diff=lfs merge=lfs -text
*.wav filter=lfs diff=lfs merge=lfs -text
*.fbx filter=lfs diff=lfs merge=lfs -text
*.blend filter=lfs diff=lfs merge=lfs -text lockable
Art/Source/** filter=lfs diff=lfs merge=lfs -text lockable
Art/Source/*.txt -filter -diff -merge text
EOF
for f in Art/hero.png Art/Hero.PNG Audio/jump.wav Art/Source/hero.blend Art/Source/hero.kra Art/Source/notes.txt src/player.cs; do
printf '%-22s filter=%-12s lockable=%s\n' "$f" \
"$(git check-attr filter -- "$f" | cut -d' ' -f3)" "$(git check-attr lockable -- "$f" | cut -d' ' -f3)"
doneArt/hero.png filter=lfs lockable=unspecified
Art/Hero.PNG filter=unspecified lockable=unspecified
Audio/jump.wav filter=lfs lockable=unspecified
Art/Source/hero.blend filter=lfs lockable=set
Art/Source/hero.kra filter=lfs lockable=set
Art/Source/notes.txt filter=unset lockable=set
src/player.cs filter=unspecified lockable=unspecified
Three lessons in one table: Hero.PNG slipped through (add *.PNG or rename the file), the .kra file is covered by the folder rule even though nobody listed the extension, and the notes file is taken back out of LFS by the later, more specific line, although it is still lockable because that line didn't unset it (it would need -lockable too).
The silent failure: committing without LFS
If someone commits on a machine without Git LFS installed (or without having run git lfs install), Git has no lfs filter to run. It doesn't stop; it just stores the whole file in normal Git history. The pointer is missing, the repository grows, and nobody notices until clones get slow. You can reproduce that here, because this sandbox has no LFS:
mkdir game && cd game && git init -q
printf '*.png filter=lfs diff=lfs merge=lfs -text\n' > .gitattributes
head -c 3000 /dev/zero | tr '\0' 'P' > hero.png # stand-in for a texture
git add . && git commit -q -m "Add hero texture"
echo "blob stored for hero.png: $(git cat-file -s HEAD:hero.png) bytes"
git cat-file -p HEAD:hero.png | head -c 40; echoblob stored for hero.png: 3000 bytes
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
With LFS working, that blob would be a pointer of about 130 bytes starting with version https://git-lfs.github.com/spec/v1. A check in CI catches the mistake by looking at every file that should be in LFS:
mkdir game && cd game && git init -q
printf '*.png filter=lfs diff=lfs merge=lfs -text\n' > .gitattributes
printf 'version https://git-lfs.github.com/spec/v1\noid sha256:%064d\nsize 3000\n' 0 > ok.png
head -c 3000 /dev/zero | tr '\0' 'P' > raw.png
git add . && git commit -q -m "Two textures"
status=0
for path in $(git ls-files ':(attr:filter=lfs)'); do
if git cat-file -p "HEAD:$path" | head -n1 | grep -q '^version https://git-lfs'; then
echo "ok $path"
else
echo "NOT LFS $path"; status=1
fi
done
echo "exit status $status"ok ok.png
NOT LFS raw.png
exit status 1
(ok.png was written as a pointer by hand to stand in for a properly tracked file.) :(attr:filter=lfs) is Git's attribute pathspec magic: it selects files by their attributes rather than their names. The Git LFS client also has git lfs fsck --pointers for the same purpose in recent versions; the plain-Git script works anywhere.
Day-to-day commands
These need the git-lfs client, so they are shown as a sample session:
git lfs install # once per machine: registers the filters and hooks
git lfs track "*.psd" --lockable # appends a rule to .gitattributes
git lfs ls-files # which committed files are LFS objects
git lfs ls-files --size # ... with sizes
git lfs migrate info --everything # which file types take the most space in history
git lfs migrate import --include="*.psd" --everything # rewrite history to move them into LFS
git lfs prune # delete old local LFS objects you no longer needgit lfs migrate import rewrites history (every commit hash changes), so coordinate with the team, and do it before many people have cloned, or better, set up LFS before the first binary commit.
Fast clones and CI
LFS downloads the content for the commit you check out, which for a big game is still gigabytes. Options:
- Skip the download and fetch what you need:
GIT_LFS_SKIP_SMUDGE=1 git clone …, thengit lfs pull --include="Assets/Levels/Level1/**". - Limit what is fetched permanently with
git config lfs.fetchinclude "Assets/UI/**,Assets/Characters/**"(andlfs.fetchexclude). - Cache LFS objects in CI: GitHub's
actions/checkouthas anlfs: trueoption; cache.git/lfsbetween runs so every build doesn't re-download every texture (see GitHub Actions & GameCI). - Watch your quota: hosting providers meter LFS storage and bandwidth separately from the repository, and CI downloads count. Check your host's current limits and pricing.
Test yourself
On a Linux CI machine, does the rule *.png filter=lfs ... match Hero.PNG?
- Yes, always
- No, patterns are case-sensitive there
- Only in Unity projects
- Only after git lfs install
What happens when someone without Git LFS commits a file that .gitattributes assigns to LFS?
- Git refuses the commit
- Git stores the full file in normal history, silently
- The file is skipped
- Git installs LFS automatically
What does the pathspec ':(attr:filter=lfs)' select?
- Files whose names contain lfs
- Files whose attributes set filter=lfs
- Files in an lfs folder
- Locked files