Sub topic 2
How Git LFS Works: Pointer Files and a Separate Object Store
This chapter explains the working model behind Git LFS. It assumes you can clone, add, commit, push, and pull, that you understand branches and remotes, and that you know what a .gitattributes file does. You do not need to install anything yet.
The core idea
Git LFS (Large File Storage) changes what Git stores for a large file. Git keeps a small text file called a pointer file in the commit. The real content goes to a separate LFS object store on a server. Your repository history stays small, and the large content is downloaded only for the versions you check out.
A pointer file looks like this:
version https://git-lfs.github.com/spec/v1
oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393
size 12345It has three lines:
versionnames the pointer format.oidis the SHA-256 hash of the real content. LFS uses it as the object's ID and as its storage key.sizeis the real file size in bytes.
A pointer is about 130 bytes, whatever the size of the real file.
Which files become pointers
Git decides by reading .gitattributes. Running git lfs track "*.psd" writes this line:
*.psd filter=lfs diff=lfs merge=lfs -textThe filter=lfs part tells Git to pass matching files through the LFS filter. The -text part tells Git to treat them as binary and not to convert line endings. The diff=lfs and merge=lfs parts select LFS-aware diff and merge drivers. Because .gitattributes is committed, everyone who clones the repository gets the same rules.
Clean and smudge filters
The conversion happens through two Git filters that LFS registers.
- The clean filter runs when you
git add. It hashes the real file, copies the content into the local LFS store under.git/lfs/objects, and gives Git the pointer text to record. - The smudge filter runs when Git writes a file into your working tree, for example on checkout. It reads the pointer, finds the content in the local store (or downloads it), and writes the real file.
So your working tree holds real files, and the Git object database holds pointers.
The diagram below shows the full path of one file.
What happens on push
LFS installs a pre-push hook. Before Git sends your commits, the hook finds the pointers in them and uploads the matching objects to the LFS server. Then Git pushes the commits, which contain only pointers. If the upload fails, the push fails, so the remote does not end up with pointers whose content is missing.
Why history stays small
Git keeps every version of every file. A 200 MB file edited ten times adds about 2 GB to a normal repository, and every clone downloads all of it. With LFS, each version adds one pointer to the history. A clone fetches only the LFS objects needed for the commit it checks out. Older versions download later, if you check them out.
| Row | Regular Git | Git LFS |
|---|---|---|
| Stored in commit | Full file content | Pointer file |
| Content lives in | Git object database | LFS object store |
| Clone downloads | All versions | Pointers plus current versions |
| Deduplication | By Git object hash | By SHA-256 `oid` |
Because the oid is a content hash, two files with identical content share one stored object.
Common errors at this level
- Pointer text in your working file. If the smudge filter did not run, you see the three-line pointer instead of the real file. This happens when LFS is not installed for your user, or when a clone skipped LFS content.
- Tracking added after the file was committed.
git lfs trackaffects files added from then on. Files already committed as regular blobs stay in history unchanged. - Forgetting to commit
.gitattributes. Teammates will then commit large files as regular blobs.
Exercise
A teammate sends you a file called logo.psd. When you open it, it contains three lines of text starting with version https://git-lfs.github.com/spec/v1. What are you looking at, and which filter should have replaced it with the real content?
Show answer
You are looking at an LFS pointer file. The smudge filter should have replaced it with the real content on checkout. It most likely did not run because Git LFS was not installed for your user, or because the clone skipped LFS downloads.