Sub topic 4
Choosing what to track with git lfs track
This chapter assumes you can clone, add, commit, and push, and that you know what a .gitattributes file does. It also assumes Git LFS is installed and enabled with git lfs install. Here you decide which files LFS should manage and how that choice is recorded.
What git lfs track does
git lfs track does not move any files. It writes a pattern into .gitattributes. Git reads that file when you run git add. If a file matches the pattern, Git sends it through the LFS filter and stores a small pointer in the commit instead of the content.
git lfs track "*.psd"The command appends this line to .gitattributes, creating the file if needed:
*.psd filter=lfs diff=lfs merge=lfs -textThe attributes mean the following:
filter=lfsruns the LFS clean and smudge filters on matching files.diff=lfsandmerge=lfstell Git to use LFS-aware drivers for diffs and merges.-textmarks the file as binary, so Git does not convert line endings.
Run git lfs track with no arguments to print the patterns currently tracked.
Pattern syntax
Patterns follow .gitattributes rules, which are close to .gitignore rules. Quote them so your shell does not expand them before Git LFS sees them.
| Pattern | Matches |
|---|---|
| `*.psd` | Every `.psd` file at any depth |
| `design/*.psd` | `.psd` files directly inside `design/` |
| `textures/**` | Everything under `textures/` |
| `data/big.csv` | One specific file |
Prefer a few broad patterns by file type, such as *.psd, *.mp4, and *.zip. A pattern for each file is hard to maintain, and a new file that does not match will go into normal Git history.
Commit .gitattributes first
The tracking rule only affects files added after the rule exists. Commit .gitattributes before the files it covers, so teammates get the same rule when they pull.
git lfs track "*.psd"
git add .gitattributes
git add design/logo.psd
git commit -m "Add logo source file via Git LFS"
git push origin mainCommon mistakes
- Forgetting to commit
.gitattributes. Other clones will store matching files as regular Git objects. - Adding a file before tracking it. If you ran
git addfirst, unstage the file withgit rm --cached, rungit lfs track, then add it again. - Unquoted wildcards. Without quotes, the shell may expand
*.psdinto a list of the current directory's files, and only those names get tracked. - Tracking too much. Source code and small text files diff and compress well in Git. LFS gives no benefit there and adds a server dependency.
- Editing patterns by hand incorrectly. You can edit
.gitattributesdirectly, but you must keep the full attribute set.git lfs trackwrites it correctly for you.
To stop tracking a pattern, run git lfs untrack "*.psd". This removes the line from .gitattributes. Files already stored as pointers remain pointers until you change and re-add them.
Check your rule
After tracking a pattern, confirm it applies to a file before you commit:
git check-attr filter -- design/logo.psdThe output design/logo.psd: filter: lfs shows the file will go through LFS. If it prints unspecified, no pattern matches.
Exercise
You run git add assets/intro.mp4, then git lfs track "*.mp4", and commit. The video is still stored as full content in Git. What went wrong, and how do you fix it before pushing?
Show answer
The file was added before the tracking rule existed, so the staged entry used the normal Git path. Unstage it with git rm --cached assets/intro.mp4, make sure .gitattributes is staged, then run git add assets/intro.mp4 again and commit. If you already committed, amend or reset that local commit first. Check with git lfs ls-files that the file is listed.
Exercise
A teammate pulls your branch and their new .psd file is committed as a regular Git object. Your own commits used LFS correctly. What is the most likely cause?
Show answer
They did not have the .gitattributes rule, or Git LFS was not enabled on their machine. Confirm that .gitattributes is committed and pulled, and ask them to run git lfs install.