Sub topic 5
Committing and Pushing LFS Files
This chapter covers what happens when you commit and push a file that Git LFS tracks. It assumes you can already use git add, git commit, and git push, and that you know what .gitattributes does. Earlier chapters covered pointer files and installation. Here you will see how the pieces work together during everyday use.
The Everyday Workflow in Commands
The sequence below sets up LFS, tracks a pattern, and pushes a file.
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 mainThe commands do the following:
git lfs installis a one-time setup per user account. It registers the LFS filters in your global Git configuration and installs hooks in the current repository.git lfs track "*.psd"writes a rule to.gitattributes. It does not touch any file that is already committed.git add .gitattributesstages the rule. Commit this file so that every collaborator gets the same rule.git add design/logo.psdstages the file. The LFS filter runs at this point.git commitrecords a commit that contains a pointer file, not the real image data.git pushuploads the real content to the LFS server, then sends the commit to the Git remote.
The rule that step 2 writes looks like this:
*.psd filter=lfs diff=lfs merge=lfs -textEach attribute has a job:
filter=lfsruns the clean and smudge filters on matching files.diff=lfsandmerge=lfstell Git to use LFS-aware diff and merge handling.-textmarks the file as binary, so Git never converts line endings.
Clean and Smudge Filters
A filter is a pair of programs that Git runs when content moves between your working tree and the repository.
- The clean filter runs when you
git add. It reads the real file, stores the content in the local LFS cache, and outputs a small pointer. Git stores the pointer. - The smudge filter runs when Git writes a file into your working tree, for example during
git checkout. It reads the pointer and replaces it with the real content from the local cache. If the content is not cached, it downloads it from the LFS server.
The pointer file that Git stores looks like this:
version https://git-lfs.github.com/spec/v1
oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393
size 12345The three lines give the pointer spec version, the SHA-256 hash of the real content, and its size in bytes. The hash is also the object's identifier in the LFS store. Two files with identical content produce the same pointer and share one stored object.
What Happens During a Push
Git LFS installs a pre-push hook. When you run git push, Git runs the hook before it sends any commits. The hook works out which LFS objects the remote does not have yet and uploads them. Only after the upload succeeds does Git send the commits.
This order matters. A commit that references a pointer is never published before its content is available. If the LFS upload fails, the push stops, and collaborators are not left with commits that point to missing objects.
Only objects the server lacks are uploaded. If you push a branch and the server already holds some of those objects, they are skipped.
The ordering also explains a common error. If you clone or push using a tool that bypasses Git hooks, the pointer commits can reach the remote without their content. Later chapters cover diagnosing that case.
Order of Operations Matters
Tracking only affects files that are added after the rule exists. Consider this sequence:
git add design/logo.psd
git lfs track "*.psd"
git commit -m "Add logo"The file was staged before the rule existed, so Git staged the full binary. The commit stores the real content in regular Git history. To fix a mistake like this before you push, unstage the file and add it again:
git rm --cached design/logo.psd
git add design/logo.psd
git commit --amendIf the commit is already shared, amending is not safe. Moving files that are already in history into LFS is a history rewrite, which a later chapter covers.
Verifying That a File Went Through LFS
After you commit, check that Git stored a pointer. These commands show the committed content and the files LFS manages:
git show HEAD:design/logo.psd
git lfs ls-filesgit show HEAD:design/logo.psd prints the stored blob. For an LFS file, the output is the three-line pointer, not binary data. git lfs ls-files lists each LFS file with a short object ID. A * after the ID means the real content is present in your working tree. A - means only the pointer is present.
Changing a File
When you edit a tracked file and run git add again, the clean filter computes a new SHA-256 and writes a new pointer. The new content is stored as a separate object. The old object stays in the store, because older commits still point to it.
Each saved version of a large file therefore adds its full size to LFS storage. LFS avoids bloating your Git history, but it does not compress or delta-store the versions of a binary the way Git packfiles can for text. Plan storage on that basis. Frequent small edits to a large asset add up.
Pushing Several Branches
git push uploads LFS objects for the refs you push. Pushing all refs uploads the LFS objects for all of them:
git push origin --allIf you want to upload objects without pushing commits, for example to prepare a server before a migration, use:
git lfs push origin mainA related option is git lfs push --all origin, which uploads every LFS object reachable from all refs.
Edge Cases
- Empty files. An empty file is not converted to a pointer. Git stores it as it is.
- Files that match the pattern in a subdirectory. A pattern without a slash, such as
*.psd, matches at every depth. A pattern with a path, such asdesign/*.psd, matches only that directory. - Editors that replace files. Some tools write a new file instead of editing in place. This is fine for LFS, because the clean filter runs on the final content at
git add. - Interrupted uploads. If a push fails during upload, run
git pushagain. LFS retries only the missing objects. - Authentication. LFS often uses the same credentials as Git over HTTPS. With SSH remotes, LFS may still request a token over HTTPS. A failure at the upload step with an authorization error usually means the credential helper has no valid token for the LFS endpoint.
Common Mistakes
| Mistake | Symptom | Fix |
|---|---|---|
| Staged file before running `git lfs track` | `git show HEAD:path` prints binary data | Unstage with `git rm --cached`, add again, amend |
| Forgot to commit `.gitattributes` | Teammates commit full binaries | Commit and push the rules file |
| Skipped `git lfs install` | Pointer-free commits, no hook output on push | Run `git lfs install`, then re-add the files |
| Used a pattern with the wrong path | File not listed by `git lfs ls-files` | Run `git lfs track` to review patterns and correct them |
A Short Practice Session
Try this in a scratch repository to see the whole flow:
git init lfs-demo
cd lfs-demo
git lfs install --local
git lfs track "*.bin"
head -c 1048576 /dev/urandom > sample.bin
git add .gitattributes sample.bin
git commit -m "Add sample binary"
git show HEAD:sample.bin
git lfs ls-filesThe git show output is a pointer with a size of 1048576. The ls-files output lists sample.bin. Without a remote you cannot push, but the clean filter and the pointer behave the same as they would with one.
Exercise
You run git add logo.psd, then git lfs track "*.psd", then git commit. git show HEAD:logo.psd prints binary data. Why did this happen, and what commands fix it before you push?
Show answer
The file was staged before the tracking rule existed, so the clean filter did not run and Git staged the full binary. The .gitattributes rule only applies to files added after it is in place. Fix it with git rm --cached logo.psd, then git add .gitattributes logo.psd, then git commit --amend. Afterwards git show HEAD:logo.psd should print the three-line pointer.
Exercise
During git push, the LFS upload fails with a network error. Does the remote receive commits that reference the missing objects? Explain.
Show answer
No. The pre-push hook uploads LFS objects before Git sends commits, and a hook failure aborts the push. The remote receives no commits that point to content it lacks. Running git push again uploads only the missing objects.