Sub topic 5
Committing and pushing LFS files
This chapter assumes you have installed Git LFS, run git lfs install once for your user, and know what git lfs track writes to .gitattributes. You should also be comfortable with git add, git commit, and git push.
What happens when you run git add
Git LFS plugs into Git through filters named in .gitattributes. A tracked pattern looks like this:
*.psd filter=lfs diff=lfs merge=lfs -textWhen you stage a matching file, Git runs the LFS clean filter. The filter does three things:
- It computes the SHA-256 hash of the real file.
- It copies the real content into the local LFS store, under
.git/lfs/objects. - It hands Git a small pointer file to stage in place of the content.
The commit therefore holds only the pointer:
version https://git-lfs.github.com/spec/v1
oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393
size 12345Your working tree still shows the real file. The reverse step, the smudge filter, swaps a pointer for the real content when you check out a commit.
The push step
git lfs install also adds a pre-push hook. When you run git push, the hook runs first. It finds the LFS objects referenced by the commits you are about to send and uploads them to the LFS server. Git then pushes the commits, which contain only pointers.
A complete example
git lfs install
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 mainStage .gitattributes first or together with the binary. The file holds the tracking rule, so everyone who clones the repository needs it.
During the push, Git LFS prints upload progress before the usual Git output. A line such as Uploading LFS objects: 100% (1/1) confirms the content reached the server.
Verifying before you push
Check that the file will go through LFS before you commit:
git lfs status
git lfs ls-filesgit lfs status shows staged files as LFS: <oid> when the clean filter ran. git lfs ls-files lists the files LFS manages. If your file does not appear, the pattern did not match, or the file was committed before tracking began.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| File is committed as a full blob | Tracking rule added after `git add`, or hooks not installed | Run `git lfs install`, then `git rm --cached` the file and `git add` it again |
| Teammates get pointer text instead of content | `.gitattributes` was not committed or pushed | Commit `.gitattributes` and push |
| Push is rejected with an LFS error | Server quota or permission problem | Check storage limits and your access to the repository |
| `git lfs ls-files` is empty | Pattern does not match the path | Run `git lfs track` to list patterns and fix the glob |
Pushing in practice
You can push as usual to any branch. Objects upload once, because the server stores them by SHA-256 hash. Pushing a second branch that references the same content does not upload it again. If an upload fails midway, rerun git push and the hook resumes with objects the server does not yet have.
Exercise
You ran git add design/logo.psd and committed it, then ran git lfs track "*.psd". git lfs ls-files does not list the file. Why, and what is the smallest fix for the latest commit if you have not pushed?
Show answer
The file was staged before the tracking rule existed, so the clean filter did not run and Git stored the full content as a normal blob. Track the pattern, commit .gitattributes, then run git rm --cached design/logo.psd, git add design/logo.psd, and git commit --amend (or make a new commit). If the file is already in earlier pushed history, use git lfs migrate import instead.