Sub topic 4
Choosing what to track with git lfs track
This chapter assumes you have read the earlier chapters on pointer files and installing Git LFS. You should be comfortable with git add, git commit, and .gitattributes, and git lfs install should already have run on your machine. Here you will learn how git lfs track works, how to write good patterns, and how to decide what belongs in LFS.
What git lfs track actually does
git lfs track does not move any data. It edits a .gitattributes file. Git reads the attributes in that file when it stages a file. If a file matches a pattern that has filter=lfs, the LFS clean filter replaces the content with a pointer file.
git lfs track "*.psd"This appends one line to .gitattributes in the current directory:
*.psd filter=lfs diff=lfs merge=lfs -textEach attribute has a job:
| Attribute | Effect |
|---|---|
| `filter=lfs` | Runs the LFS clean filter on add and the smudge filter on checkout. |
| `diff=lfs` | Lets `git diff` show pointer changes rather than binary noise. |
| `merge=lfs` | Uses the LFS merge driver, which treats the file as unmergeable content. |
| `-text` | Turns off line-ending conversion, which would corrupt binary data. |
Because the rule lives in .gitattributes, it is an ordinary versioned file. Commit it, and everyone who clones the repository gets the same rules.
git lfs track "*.psd"
git add .gitattributes
git commit -m "Track PSD files with Git LFS"Commit .gitattributes before or together with the first large file. If the rule is not committed, a teammate who adds a matching file will commit the full binary into normal Git history.
Quote your patterns
Always put the pattern in quotes. Without quotes, your shell expands *.psd into the matching file names in the current directory. Git LFS would then track those specific files and ignore future ones.
# Wrong: the shell expands this to "logo.psd banner.psd" if both exist
git lfs track *.psd
# Right: LFS receives the literal pattern
git lfs track "*.psd"Pattern rules
Patterns in .gitattributes follow the same matching rules as .gitignore, with some differences. A few behaviors matter most when tracking files.
- A pattern with no slash, such as
*.psd, matches in every directory below the.gitattributesfile. - A pattern containing a slash, such as
textures/*.png, is anchored to the directory that holds the.gitattributesfile. - Negated patterns (starting with
!) are not supported in.gitattributes. You cannot write "track everything except X". - Matching is case-sensitive on most setups.
*.psddoes not matchLOGO.PSD. - A pattern matches a file name. Tracking a directory name alone does not do what you expect, so use a glob such as
assets/**.
Some common examples:
git lfs track "*.psd"
git lfs track "*.{zip,tar.gz}"
git lfs track "datasets/*.parquet"
git lfs track "assets/**"
git lfs track "*.mp4" "*.mov"Brace expansion like {zip,tar.gz} is not part of .gitattributes syntax. Some shells expand it before Git LFS sees it, which gives you two separate arguments, and others pass it through unchanged and LFS then writes a literal pattern that matches nothing. Writing one pattern per extension avoids the ambiguity.
git lfs track "*.zip" "*.gz"Listing and removing patterns
Run git lfs track with no arguments to print every pattern in effect, along with the file that defines it:
git lfs trackTo stop tracking a pattern, use untrack:
git lfs untrack "*.psd"
git add .gitattributesUntracking changes the rule for future commits only. Files already committed as pointers stay pointers in history. Files in the working tree are not converted back until you re-add them.
Where the .gitattributes file lives
By default, git lfs track writes to .gitattributes in your current working directory. This leads to two common outcomes.
- Run it from the repository root, and one root file holds all rules. This is the easiest layout to review.
- Run it from a subdirectory, and a new
.gitattributesappears there. Its patterns apply only to that subtree.
Nested files are legitimate. A game repository might keep art/.gitattributes and audio/.gitattributes owned by different teams. The cost is that rules become scattered, and git lfs track from the root only lists what it can find. If you want one place for all rules, run the command from the root every time.
Tracking by extension, directory, or exact name
There are three useful scopes, and each has trade-offs.
| Scope | Example | Good for | Risk |
|---|---|---|---|
| Extension | `*.psd` | Formats that are always large | Catches small files too, such as a 4 KB icon |
| Directory | `assets/**` | Folders that hold only heavy content | Misses large files placed elsewhere |
| Exact file | `models/base.bin` | One known large file | Does not cover renamed or new files |
Tracking by extension is the usual default because it needs no maintenance as the team adds files. Tracking by directory is a better fit when a folder mixes formats and you want all of it in LFS, such as raw captures with several extensions.
If a file name contains characters that have special meaning in patterns, such as [, ], or *, use --filename to treat the argument as a literal name:
git lfs track --filename "exports/report[final].pdf"Choosing what belongs in LFS
LFS adds some cost. Every contributor needs the extension installed, the server must support it, and many hosts apply storage and bandwidth quotas. Track a file type when most of these are true:
- The file is binary, so Git cannot store small deltas between versions.
- Individual files are large, commonly several megabytes or more.
- The file changes often, so history would accumulate many full copies.
- You do not need line-by-line diffs or text merges.
Good candidates include design files (.psd, .sketch, .fig exports), video and audio, compiled game assets, ML model weights, large datasets, and archives. Poor candidates include source code, small images such as icons, JSON or CSV files that people review in diffs, and lockfiles.
Be careful with formats that are binary in practice but small. Tracking thousands of 2 KB PNG files in LFS makes each clone make many small object requests and gains almost nothing in repository size. Use a directory-scoped pattern, or leave small images in regular Git.
Tracking only affects files added afterward
This is the most common surprise. git lfs track changes how Git treats files when they are staged. It does not rewrite files that are already committed.
Suppose design/logo.psd was committed last month as a normal blob. You now run git lfs track "*.psd". The existing commit still contains the full binary. If you edit and commit the file again, the new version is stored as an LFS pointer, but the old binary stays in history and still bloats every clone.
Two different fixes exist, and they solve different problems.
- To convert files in your working tree so the next commit stores pointers, re-stage them:
git add --renormalize .. The old history is unchanged. - To remove the large blobs from history, use
git lfs migrate import, covered in a later chapter. This rewrites commits.
You can check which state a file is in with git lfs ls-files. It lists only files that are stored as LFS objects.
Verifying a rule before you commit
Before committing a large batch, confirm that Git applies the rule you intended. git check-attr reports the attributes for a path:
git check-attr filter diff merge -- design/logo.psdExpected output when the rule matches:
design/logo.psd: filter: lfs
design/logo.psd: diff: lfs
design/logo.psd: merge: lfsIf the output says unspecified, no rule applies. Likely causes are a misspelled pattern, a rule saved in a different directory's .gitattributes, or a file name that differs in case.
After staging, git lfs status shows whether the staged file will be committed as an LFS object. Checking at this point is cheaper than discovering the mistake after a push.
git add design/logo.psd
git lfs statusLocking and the lockable flag
Tracking can also mark a pattern as lockable:
git lfs track --lockable "*.psd"This adds a lockable attribute to the rule. Matching files become read-only in the working tree until someone takes a lock. Binary files cannot be merged, so locking prevents two people from making edits that conflict. The lock commands are covered in a later chapter. For now, note that the flag is chosen at tracking time, so decide it when you write the rule.
Keeping rules maintainable
A few habits keep tracking rules from becoming a problem:
- Keep all rules in one root
.gitattributesunless separate teams own separate subtrees. - Add a comment above groups of rules. Lines starting with
#are allowed in.gitattributes, butgit lfs trackmay reorder or rewrite lines, so check the diff after running it. - Review
.gitattributeschanges in pull requests as carefully as code. A broad pattern such as*or*.jsonchanges how every contributor's repository behaves. - Document the tracked types in the README so new contributors know to install LFS first.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Tracked only a few files, new ones are not caught | Pattern was unquoted and the shell expanded it | `git lfs untrack` the file names, then track `"*.ext"` |
| `git check-attr` says unspecified | Pattern is in a different directory's `.gitattributes`, or the case differs | Run from the right directory, or add a pattern for the other case |
| Repository is still large after tracking | Old blobs remain in history | Use `git lfs migrate import` |
| Rule works for you, not a teammate | `.gitattributes` not committed, or LFS not installed for them | Commit the file; run `git lfs install` |
| Small files slow down clones | Extension rule catches many tiny files | Scope the pattern to a directory |
Exercise
You run git lfs track *.psd without quotes in a folder holding a.psd and b.psd. What does .gitattributes contain, and why does that matter for the next file, c.psd?
Show answer
The shell expands the pattern first, so LFS receives the two names a.psd and b.psd and writes one rule for each. A new file named c.psd matches neither rule, so it is committed as a normal blob. The fix is to run git lfs untrack a.psd b.psd and then git lfs track "*.psd", with quotes.
Exercise
A binary file was committed last month as a normal blob. You then add a tracking rule and commit a new version of the file. Is the repository smaller? What would you use to shrink it?
Show answer
No. The new version is stored as a pointer, but the old full binary remains in history, so clones still download it. Use git lfs migrate import --include="*.ext" to rewrite history so old versions become pointers. This changes commit IDs and requires a coordinated force push.
Exercise
Which command confirms that Git will apply the LFS filter to assets/intro.mov before you commit?
Show answer
git check-attr filter -- assets/intro.mov should print filter: lfs. After git add, git lfs status also shows the file as an LFS object to be committed.