Sub topic 12
Git LFS and the filter attribute
This chapter assumes you can write a .gitattributes file, you know what the binary macro and the -text attribute do, and you have Git LFS installed. Earlier chapters covered keeping Git from converting binary files. Large files raise a different problem: even unconverted, every version of a 200 MB design file is stored in the repository forever, and every clone downloads all of them.
What the filter attribute does
The filter attribute names a pair of commands that Git runs when it moves content between the working tree and the repository:
- The clean command runs when a file is staged. It transforms the working-tree content into what Git stores.
- The smudge command runs on checkout. It transforms the stored content back into the working-tree file.
Git LFS is the most common user of this mechanism. Its clean filter swaps the real file for a small pointer file. Its smudge filter reads the pointer and downloads the real content.
A pointer file looks like this:
version https://git-lfs.github.com/spec/v1
oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393
size 84213907Git commits only these three lines. The large content goes to an LFS store, which is usually the hosting service.
Setting up LFS tracking
Install the filters once per user, then track a pattern in each repository:
# One-time setup per user: registers the clean and smudge filters in your Git config
git lfs install
# Track a pattern in this repository
git lfs track "*.psd"
# The rule lives in .gitattributes, so commit it
git add .gitattributes
git commit -m "Track PSD files with Git LFS"The git lfs track command writes this line to .gitattributes:
# Store large design files with Git LFS
*.psd filter=lfs diff=lfs merge=lfs -textEach part of the line has a job:
| Part | Effect |
|---|---|
| `filter=lfs` | Routes the file through the LFS clean and smudge commands. |
| `diff=lfs` | Uses the LFS diff driver, so diffs compare content and not pointer text. |
| `merge=lfs` | Uses the LFS merge driver for these files. |
| `-text` | Unsets the `text` attribute, so no line-ending conversion touches the file. |
Because -text is part of the rule, a * text=auto line earlier in the file cannot convert LFS-tracked files. The later, more specific rule wins for that attribute.
Checking that it works
Use the inspection tools from earlier chapters, plus an LFS-specific one:
# Confirm the attributes that apply to a path
git check-attr filter diff merge text -- art/logo.psd
# List files that Git LFS manages, with a short object ID
git lfs ls-files
# Show how Git LFS sees the repository
git lfs statusCommon errors
- Tracking after the fact.
git lfs trackonly affects files staged afterward. Files already committed stay in history as full blobs. To rewrite history so past commits use pointers, rungit lfs migrate import --include="*.psd". This rewrites commits, so coordinate with your team before pushing. - Forgetting to commit
.gitattributes. Without the rule, teammates' Git stores the large file normally. - Pointer text in the working tree. If a file opens as three lines starting with
version https://git-lfs..., the smudge step did not run. Usually Git LFS is not installed on that machine, orgit lfs installwas never run. Install it, then rungit lfs pull. - Pattern mismatch.
*.psddoes not match*.PSDon a case-sensitive file system. Add a rule for each spelling you use.
Exercises
Exercise
A teammate clones your repository and finds that hero.psd is a three-line text file starting with version https://git-lfs.github.com/spec/v1. What is the likely cause, and what two commands fix it?
Show answer
Git LFS is not installed or its filters were not set up on their machine, so the smudge step never replaced the pointer with the real file. After installing Git LFS, run git lfs install and then git lfs pull to download the content and replace the pointers.
Exercise
Your .gitattributes has * text=auto first and *.psd filter=lfs diff=lfs merge=lfs -text later. Why is the file not converted to LF, and how would you verify it for art/hero.psd?
Show answer
Later rules override earlier ones for the same attribute, so -text from the PSD rule replaces text=auto. Verify with git check-attr text filter -- art/hero.psd, which should report text: unset and filter: lfs.