Sub topic 11
Checking out LFS files in CI
A CI job starts from a clean machine. It has no local LFS cache, no credentials you typed earlier, and often no human watching. Git LFS (Large File Storage) works correctly in that setting only if you ask for the LFS content on purpose. This chapter covers how to configure that, how to download only what a job needs, and how to confirm the files are real.
This chapter assumes you know the following:
- What pointer files are and how the clean and smudge filters work.
- What
.gitattributestracking patterns do. - How
git lfs pulldiffers fromgit pull. - Basic CI vocabulary: job, step, runner, and workflow file.
Why CI needs special handling
On a developer laptop, git lfs install has already run, so the smudge filter turns pointer files into real content during checkout. A CI runner may fail in one of three ways:
- Git LFS may not be installed on the runner.
- The checkout step may skip the smudge filter.
- The credentials the checkout uses may not cover the LFS endpoint.
In each case the job still succeeds at the Git level. The workspace contains pointer files, which are small text files starting with version https://git-lfs.github.com/spec/v1. A build that expects a PNG, a model, or a dataset then fails later with a confusing error. Examples are "invalid image header", "corrupt archive", or a test comparing a 130-byte file to a golden file.
The rest of this chapter makes sure LFS content arrives, and that you notice when it does not.
The checkout flow
A CI checkout always does two things. It fetches Git objects, then it materializes the working tree. LFS adds a third thing, which is downloading the objects the pointers refer to.
The "No" branch on the first decision is not an error. Skipping LFS download is a legitimate optimization for jobs that never read the large files, such as linting or unit tests.
GitHub Actions
The actions/checkout action downloads LFS content only when you set lfs: true:
steps:
- uses: actions/checkout@v4
with:
lfs: trueGitHub-hosted runners already have the git-lfs binary. Self-hosted runners may not, so install it as part of the runner image.
With lfs: true, the action fetches every LFS object that the checked-out commit references. That is every file matching your tracking patterns, in the current version only. It does not download older versions from history.
Shallow clones
actions/checkout defaults to fetch-depth: 1, which makes a shallow clone containing one commit. LFS works with shallow clones because it needs only the pointer files in that one commit. You do not need fetch-depth: 0 for LFS.
You do need full history if a step runs git lfs fetch --all or compares old LFS versions. That is rare in CI.
Token permissions
The default GITHUB_TOKEN can read LFS objects in its own repository when the job has contents: read. If you set a permissions: block for the workflow, keep that permission:
permissions:
contents: readIf your LFS objects live in a different repository, such as a shared asset repository, the default token cannot read them. Use a personal access token or a GitHub App token for that repository, passed to the checkout through the token input.
Downloading only part of the LFS content
Many repositories hold several gigabytes of LFS data, and a given job usually needs a small slice. The partial-clone pattern from the cloning chapter applies in CI. Skip LFS during checkout, then pull selected paths.
steps:
- uses: actions/checkout@v4
with:
lfs: false
- name: Download only texture assets
run: git lfs pull --include="textures/"
- name: Run texture tests
run: ./scripts/test-textures.shThe checkout action leaves its credentials in the repository's Git configuration by default, so git lfs pull authenticates without extra setup. If you set persist-credentials: false, this step fails with an authorization error.
You can also exclude paths. Quote the patterns and use --exclude when the job needs everything except one heavy directory:
git lfs pull --exclude="video/,raw-captures/"Paths in --include and --exclude are comma-separated, and each is matched against the paths of the LFS files.
Other CI systems
The idea is the same everywhere. Install git-lfs, make sure the filter is enabled, and decide when to download.
GitLab Runner downloads LFS objects during its clone and fetch step when git-lfs is installed in the executor environment. To skip that and pull selectively, set GIT_LFS_SKIP_SMUDGE for the job:
texture-tests:
variables:
GIT_LFS_SKIP_SMUDGE: "1"
script:
- git lfs install --local
- git lfs pull --include="textures/"
- ./scripts/test-textures.shGIT_LFS_SKIP_SMUDGE=1 tells the smudge filter to leave pointers in place. The git lfs install --local line makes sure the hooks and filters exist in this repository's configuration. It is harmless if they are already there.
In a generic shell-based pipeline such as Jenkins or a container build, you can run these commands yourself:
git lfs install --local
git clone https://example.com/team/assets.git
cd assets
git lfs pullCaching LFS objects between runs
Every full LFS download counts against your host's bandwidth quota, and every CI run on a fresh runner triggers one. A cache avoids repeated downloads.
LFS stores downloaded objects under .git/lfs. If you restore that directory before pulling, git lfs pull finds the objects locally and downloads only what is missing. Key the cache on the set of object IDs the commit references, so it changes only when LFS content changes:
steps:
- uses: actions/checkout@v4
with:
lfs: false
- name: Record LFS object IDs
run: git lfs ls-files -l | cut -d' ' -f1 | sort > .lfs-assets-id
- uses: actions/cache@v4
with:
path: .git/lfs
key: lfs-${{ hashFiles('.lfs-assets-id') }}
- name: Download LFS content
run: git lfs pullgit lfs ls-files -l prints the full SHA-256 object ID first on each line. Because lfs: false leaves pointers in the workspace, the listing works before any download happens. Hashing the sorted list gives a stable key.
Caches take space and have size limits on most platforms. Cache only if the download time or bandwidth is a real cost for you.
Verifying that content is real
A job that silently builds with pointer files is worse than one that fails. Add a guard step before the build. Two checks are cheap.
The first check uses git lfs ls-files. The character after the object ID is * when the content is present in the working tree and - when only the pointer is. This guard fails if any file is still a pointer:
if git lfs ls-files | grep -q ' - '; then
echo "LFS content missing for:" >&2
git lfs ls-files | grep ' - ' >&2
exit 1
fiThe second check inspects a specific file directly:
if head -c 40 design/logo.psd | grep -q "git-lfs.github.com"; then
echo "design/logo.psd is still a pointer file" >&2
exit 1
fiFor partial downloads, apply the first check only to the paths the job needs. Use git lfs ls-files --include="textures/" or similar, since files you deliberately skipped will correctly show -.
Common CI failures
| Symptom | Likely cause | Fix |
|---|---|---|
| Files are about 130 bytes and start with `version https://git-lfs...` | Checkout ran without LFS download | Set `lfs: true`, or run `git lfs pull` |
| `git: 'lfs' is not a git command` | `git-lfs` is not installed on the runner | Install it in the runner image or add an install step |
| `Smudge error` or `Object does not exist on the server` | The commit references an object that was never pushed | Push from the original clone with `git lfs push --all origin`, or recommit the file |
| HTTP 401 or 403 from the LFS endpoint | Token lacks access, or credentials were not persisted | Grant read access, or keep `persist-credentials` enabled |
| HTTP 403 or 429 about quota or bandwidth | Host's LFS bandwidth quota is used up | Cache objects, download partially, or buy more quota |
| Pull request from a fork fails on LFS | Objects live in the fork, not the base repository | Check how your host resolves fork LFS objects, and test with a branch in the main repository |
For hard cases, rerun the failing step with GIT_TRACE=1 GIT_TRANSFER_TRACE=1 set. This prints each request LFS makes, which shows the endpoint, the status code, and which object failed.
Edge cases worth knowing
- Submodules.
actions/checkoutdoes not download LFS content for submodules unless the submodules are checked out through the same step. Setsubmodules: trueandlfs: truetogether, and confirm that the token can read the submodule repositories. - Containers. If a Docker build copies the repository into the image with
COPY, the build context must already contain real files. Pull LFS content on the runner beforedocker build. The image build itself will not run the smudge filter. - Artifacts. If one job pulls LFS content and a later job uses it, pass the files as artifacts instead of pulling again. Each pull spends bandwidth quota.
- Pushing from CI. A job that commits new tracked files needs
git-lfsinstalled and the filter enabled, or it will commit the full binaries into Git history. Rungit lfs install --localfirst.
Summary of decisions
- Use
lfs: truewhen the job reads most or all LFS files. It is the simplest option. - Use
lfs: falseplusgit lfs pull --include=...when the job needs a known subset. - Add a cache keyed on object IDs when repeated downloads cost time or bandwidth.
- Always add a guard that fails on pointer files, so a skipped download cannot pass unnoticed.
Exercise
Your workflow checks out with the default settings and a test fails with "invalid PNG header". Running head -c 60 assets/hero.png on the runner prints version https://git-lfs.github.com/spec/v1. What happened, and give two different ways to fix it.
Show answer
The checkout did not download LFS content, so the workspace holds the pointer file instead of the image. Fix 1: set lfs: true on the actions/checkout step. Fix 2: keep lfs: false and add a step running git lfs pull --include="assets/" (this needs the checkout credentials to persist). Adding a guard step based on git lfs ls-files would make the failure show up earlier.
Exercise
In a job that pulled only textures/, git lfs ls-files shows - next to files under video/. Should the pointer-file guard fail the job? How would you write the guard?
Show answer
No. Files outside the downloaded paths are expected to be pointers. Limit the guard to the paths the job needs, for example git lfs ls-files --include="textures/" | grep -q ' - ', and fail only if that command finds a match.
Exercise
Why does the cache example key on a hash of git lfs ls-files -l output instead of on the commit SHA?
Show answer
The commit SHA changes on every commit, even when no LFS content changed, so the cache would almost never hit. The list of object IDs changes only when tracked files change, so the cache is reused across many commits.