Sub topic 2
How Git LFS works: pointer files and a separate object store
This chapter explains the data model behind Git LFS. It assumes you can clone, add, commit, and push, that you know what remotes and branches are, and that you have seen a .gitattributes file. You don't need LFS installed yet. Later chapters cover setup, tracking patterns, and day-to-day commands.
The core idea
Git keeps every version of every file in the repository's history. A 200 MB binary that changes ten times adds roughly 2 GB to every clone, because Git's delta compression does little for compressed or already-encoded formats.
Git LFS avoids this by splitting each large file into two parts:
- A pointer file. This is a small text file that Git commits like any other file. It names the real content by its hash.
- An LFS object. This is the real content. It is stored outside Git's object database, on an LFS server and in a local cache.
Git's history therefore holds only pointers. The large content is downloaded only for the versions you check out.
Anatomy of a pointer file
A pointer file is plain text with three lines:
version https://git-lfs.github.com/spec/v1
oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393
size 12345| Line | Meaning |
|---|---|
| `version` | The pointer spec version. Tools use it to recognize the file as a pointer. |
| `oid sha256:...` | The object ID: the SHA-256 hash of the real file's bytes. |
| `size` | The size of the real content in bytes. |
The pointer is about 130 bytes whatever the size of the real file. A 2 GB video and a 2 MB image have pointers of nearly the same length.
You can reproduce the oid and size yourself. This script prints the pointer for any file:
import hashlib
import sys
from pathlib import Path
data = Path(sys.argv[1]).read_bytes()
print("version https://git-lfs.github.com/spec/v1")
print(f"oid sha256:{hashlib.sha256(data).hexdigest()}")
print(f"size {len(data)}")Save it as pointer.py and run python pointer.py design/logo.psd. The output should match what git lfs pointer --file=design/logo.psd prints.
The oid makes the store content-addressed. The name of an object is derived from its bytes. This has three consequences:
- Two files with identical content share one object, even under different paths or in different commits.
- Content can't change without its ID changing, so a pointer always refers to exactly one version of the data.
- After a download, the client can hash the bytes and compare them with the
oidto detect corruption.
What Git stores and what LFS stores
The same file has different representations in different places. Keeping them apart avoids most confusion.
| Location | Contents for a tracked file |
|---|---|
| Working tree | The real file, after a successful checkout. |
| Git commit (blob) | The pointer text. |
| `.git/lfs/objects/` | A local copy of the real content, named by `oid`. |
| LFS server | The real content, named by `oid`. |
To see the split, ask Git for the committed blob. It returns the pointer, not the binary:
git cat-file -p HEAD:design/logo.psdThe local cache uses the first four hex characters of the oid as two nested directory levels. The object from the example above is stored at:
.git/lfs/objects/4d/7a/4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393Because the working tree holds one copy and .git/lfs/objects holds another, an LFS file takes up disk space twice on your machine. The git lfs prune command removes cached objects that no recent commit needs.
How content moves: clean and smudge filters
Git has a general mechanism for transforming a file as it moves between the working tree and the repository. Filters are attached to paths through .gitattributes. LFS uses two of them:
- The clean filter runs when you
git adda file. It hashes the content, copies the bytes into.git/lfs/objects, and gives Git the pointer text to store. - The smudge filter runs when Git writes a file into the working tree, for example on
git checkoutorgit clone. It reads the pointer, finds the object in the local cache (or downloads it), and writes the real bytes.
A tracked pattern appears in .gitattributes like this:
*.psd filter=lfs diff=lfs merge=lfs -textfilter=lfs selects both filters. -text stops Git from normalizing line endings, which would corrupt binary data. diff=lfs and merge=lfs mark the files as LFS content for those operations.
Filters run on content passing through Git. They don't create the pointer by themselves at upload time. Uploading is a separate step.
Talking to the server: the Batch API
Pushing commits and pushing LFS content are two separate transfers, often to two different services:
- A
pre-pushhook, installed bygit lfs install, runs before Git sends your commits. It works out which LFS objects the new commits refer to and uploads them. - Git then pushes the commits, which contain only pointers.
This order matters. A pointer is never published before the content it refers to.
The hook and the client talk to the LFS server through the Batch API. The client sends one request listing the objects it wants to upload or download:
{
"operation": "upload",
"transfers": ["basic"],
"objects": [
{
"oid": "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393",
"size": 12345
}
]
}The server answers with instructions for each object:
{
"transfer": "basic",
"objects": [
{
"oid": "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393",
"size": 12345,
"actions": {
"upload": {
"href": "https://storage.example.com/objects/4d7a2146",
"header": { "Authorization": "Basic ..." },
"expires_in": 3600
}
}
}
]
}The href can point at a different host from the Git remote, such as a cloud storage bucket with a short-lived signed URL. If the server already has the object, it returns the object without an actions entry, and the client skips the transfer. That is how content addressing saves bandwidth.
By default the client finds the LFS endpoint from the remote URL. For https://example.com/team/assets.git it uses https://example.com/team/assets.git/info/lfs. A repository can override this with the lfs.url setting or a committed .lfsconfig file.
Downloads follow the same pattern with "operation": "download". The server returns download actions with URLs, and the client fetches the objects and stores them in .git/lfs/objects.
Edge cases and failure modes
A pointer where you expected a file. If the smudge filter can't get the content, Git still writes the checkout. The working tree then holds the 130-byte pointer text under the original file name. The usual causes are:
- the LFS server is unreachable or rejects your credentials
- LFS was not installed on the machine that cloned the repository
- the clone deliberately skipped LFS downloads
Tools that open the file will report it as corrupt, because the pointer isn't a valid image or archive. Fix it by running git lfs pull once the underlying problem is resolved.
A file committed before it was tracked. The clean filter only runs for paths that match a pattern in .gitattributes at the time of git add. If you commit a large file first and track it afterward, the full binary is already in a Git blob. Adding the pattern later doesn't change history. A later chapter covers rewriting history with git lfs migrate.
A pointer without its object. Suppose a commit is pushed but the content upload failed or was skipped, for example because the push bypassed the hook with --no-verify. Other people can then fetch the commit and check it out, but the download fails with an "object does not exist" error. The pointer is valid, and the data it names is not on the server.
No delta compression for large files. LFS stores each version as a complete object. Changing one byte of a 100 MB file creates a new 100 MB object. The saving comes from keeping versions out of clones you don't need, not from storing fewer bytes on the server.
Pointers merge as text, content does not. Git can merge two commits that edit the same tracked file, but the merge only sees two different pointer files and reports a conflict on those three lines. There is no meaningful way to combine two Photoshop files. You choose one version.
Hash confidence. SHA-256 identifies the content. The pointer carries no signature, and anyone who can write to the repository can point a path at any existing oid. Treat LFS as storage, not as an integrity or access boundary.
Summary of the model
- Git history contains pointer files: three lines giving a version, a SHA-256
oid, and a size. - The real bytes live in
.git/lfs/objectslocally and on an LFS server remotely, addressed byoid. - The clean filter creates pointers on
git add. The smudge filter swaps pointers for content on checkout. - The
pre-pushhook uploads content through the Batch API before Git pushes the pointers. - A missing object, a missing install, or an unreachable server leaves a pointer file in the working tree.
Exercise
You clone a repository and find that hero.png is a 130-byte text file beginning with version https://git-lfs.github.com/spec/v1. Explain what the file is, where the real image data should be, and name one way to recover it.
Show answer
The file is an LFS pointer. It is what Git committed, and it contains the SHA-256 oid and size of the real image. The real data should be in .git/lfs/objects/<first two hex chars>/<next two>/<oid> locally, and on the LFS server under the same oid. The smudge filter did not replace the pointer, probably because LFS wasn't installed at clone time or the server was unreachable. Once LFS is installed and the server is reachable, running git lfs pull downloads the object and replaces the pointer with the real file.
Exercise
A teammate commits a 50 MB file twice under different names, with identical content. How many LFS objects are uploaded, and why?
Show answer
One. The oid is the SHA-256 of the content, so both pointers contain the same oid. The client's Batch API request lists the object, and after the first upload the server reports that it already has it, so no further transfer happens.