Sub topic 7
Cloning and Fetching LFS Content, Including Partial Downloads
This chapter assumes you can clone, commit, push and pull with plain Git, and that you have already tracked files with git lfs track and pushed them. Here you will see what happens on the receiving side. You will learn how a clone gets LFS content, how to skip or limit that download, and how to repair a checkout that contains only pointer files.
What a normal clone does
After git lfs install, Git LFS registers a smudge filter. A smudge filter is a program Git runs when it writes a file into the working tree. A clone of an LFS repository then runs in two phases.
- Git downloads commits, trees and blobs. For tracked files, the blobs are small pointer files.
- During checkout, the smudge filter reads each pointer, downloads the real object from the LFS server by its SHA-256
oid, caches it under.git/lfs/objects, and writes the real content into the working tree.
Your working tree holds the real file. The Git object database holds the pointer. The LFS cache holds a second copy of the content, so a checked-out LFS file uses disk space twice.
Fetch, checkout and pull
Git LFS splits its download work into the same two steps that git pull has. The names are similar on purpose.
| Command | What it does | Plain Git analogue |
|---|---|---|
| `git lfs fetch` | Downloads objects into `.git/lfs/objects`. Does not touch the working tree. | `git fetch` |
| `git lfs checkout` | Replaces pointer files in the working tree with cached objects. Does not use the network. | `git checkout` |
| `git lfs pull` | Runs `fetch` then `checkout`. | `git pull` |
A plain git pull already runs the smudge filter for files that change, so you rarely need these commands in everyday work. They matter when you want control: to download ahead of time, to download selectively, or to repair a checkout.
By default, git lfs fetch downloads only the objects needed for the commit you have checked out. You can widen that:
# Objects for the current branch tip
git lfs fetch
# Objects for a specific remote and ref
git lfs fetch origin release/2.0
# Objects for every ref on the remote, useful before archiving or mirroring
git lfs fetch --all
# Objects for recent commits on recently updated refs
git lfs fetch --recent--recent uses the lfs.fetchrecentrefsdays (default 7) and lfs.fetchrecentcommitsdays (default 0) settings to decide what counts as recent. This is handy for a laptop that you want ready to switch between active branches while offline.
Cloning without downloading the large files
Sometimes you do not want the content at all. A build server that only compiles source, or a reviewer who only reads code, has no use for 20 GB of textures. Setting the environment variable GIT_LFS_SKIP_SMUDGE=1 tells the smudge filter to leave pointer files in place.
GIT_LFS_SKIP_SMUDGE=1 git clone https://example.com/team/assets.git
cd assets
git lfs pull --include="textures/"The clone finishes quickly and every LFS file is a pointer. The git lfs pull --include="textures/" command then downloads and checks out only the objects whose paths match textures/. The rest stay as pointers.
You can confirm the state with git lfs ls-files. A * after the short oid means the real content is present in the working tree. A - means only the pointer is there.
git lfs ls-files4d7a214614 * textures/wall.png
9be02c1a77 - audio/theme.wavIn this example, the texture was downloaded and the audio file was not.
Include and exclude patterns
Both --include and --exclude accept comma-separated lists of paths or glob patterns. They work with fetch, pull and checkout.
# Everything under two directories
git lfs pull --include="textures/,models/"
# Everything except video files
git lfs pull --exclude="*.mp4,*.mov"
# Combine them: all of textures/ except the 4K variants
git lfs pull --include="textures/" --exclude="textures/4k/"Patterns follow the same rules as .gitattributes. A trailing slash selects a directory.
Making the filter permanent
Typing --include on every pull is tedious. Two configuration keys store the filter in your repository's config, and plain git pull, git checkout and git lfs fetch then respect it.
git config lfs.fetchinclude "textures/,models/"
git config lfs.fetchexclude "*.mp4"This is a useful pattern for large monorepos: each team member configures the directories they work in and never pays for the rest. Remember that these keys filter downloads, not what is stored on the server. Anyone can still change the setting and fetch more.
Skipping the smudge filter by default
If you want pointer-only behavior for every repository on a machine, set it at the user level:
git lfs install --skip-smudgeThis changes the global smudge filter to a version that does not download. Use git lfs install --force to restore the default. For one repository only, add --local. Most people prefer the environment variable because it is explicit and visible in the command, and it is easy to forget a global setting.
Switching branches and pulling later
Branch switches and pulls behave as follows.
- If the target commit's LFS objects are already in
.git/lfs/objects, checkout copies them without network access. - If they are missing, the smudge filter downloads them one batch at a time, so the switch may pause on the network.
- Objects for files that did not change between the two commits are not downloaded again.
To avoid waiting at switch time, run git lfs fetch origin <branch> first. It stores the objects, and the later checkout is instant.
Reclaiming disk space
The LFS cache keeps every object you have ever downloaded, including those from old commits. Over time it can dwarf the working tree. git lfs prune deletes cached objects that are not referenced by the current checkout, recent commits, or commits that are not yet pushed.
git lfs prune --dry-run
git lfs pruneRun the --dry-run form first to see what would be removed. Pruning is safe for pushed content because it can be downloaded again from the server. Prune never removes objects for unpushed commits, which is the case that would lose data.
Common errors and how to read them
| Symptom | Likely cause | Fix |
|---|---|---|
| Files contain `version https://git-lfs.github.com/spec/v1` | Smudge was skipped, or LFS was not installed when you cloned | Run `git lfs install`, then `git lfs pull` |
| `git lfs pull` finishes but a file is still a pointer | The path is excluded by `lfs.fetchexclude` or not matched by `--include` | Check `git config --get-all lfs.fetchexclude`, then pull that path explicitly |
| `Object does not exist on the server` | The commit was pushed without its LFS objects, or the server deleted them | Ask the author to run `git lfs push --all origin` from a clone that has the objects |
| Checkout is slow after switching branches | Objects are downloaded on demand | Run `git lfs fetch` for that branch beforehand |
| `git lfs ls-files` shows `-` for a file you need | Object not downloaded | `git lfs pull --include="path/to/file"` |
You can see the effective settings, the endpoint and the local storage path with git lfs env. When a download fails, set GIT_TRACE=1 and GIT_CURL_VERBOSE=1 to see the requests. Take care not to paste that output into public tickets, because it can include authorization headers.
Repairing a checkout with pointer files
If a working tree contains pointers but the objects are already cached, you do not need the network. Replace the pointers from the cache:
git lfs checkoutIf the objects are not cached, use git lfs pull. If Git still reports the files as modified afterward, the cause is usually a mismatch between the pointer in the commit and the content on disk. Run git status and git lfs status to see which side differs before you commit anything.
Exercise
You cloned a repository with GIT_LFS_SKIP_SMUDGE=1. git lfs ls-files shows - next to every file. Write the commands to download only the files under models/ except those under models/archive/, then explain how to make that choice persistent for future pulls in this repository.
Show answer
Run git lfs pull --include="models/" --exclude="models/archive/". This fetches the matching objects and replaces their pointers in the working tree. To make it persistent, run git config lfs.fetchinclude "models/" and git config lfs.fetchexclude "models/archive/". Afterward, plain git pull and git lfs fetch respect those filters, and git lfs ls-files shows * only for the files that were downloaded.
Exercise
A teammate says that after switching branches, their texture files are 130-byte text files starting with version https://git-lfs.github.com/spec/v1. git lfs env shows the correct endpoint. Name two likely causes and one command that repairs the working tree without using the network if the objects are already cached.
Show answer
Likely causes are that GIT_LFS_SKIP_SMUDGE is set in their environment (or git lfs install --skip-smudge was run), and that lfs.fetchexclude matches those files. The command git lfs checkout replaces pointers with objects from .git/lfs/objects. If the objects are not cached, git lfs pull is needed instead, because it downloads them first.