Sub topic 11
Sparse Checkout in CI and Automation
This chapter assumes you can run git sparse-checkout set in cone mode and know what a partial clone (--filter=blob:none) is. It covers how to combine them in CI jobs and scripts, and where those jobs usually break.
Why CI benefits most
A CI job starts from an empty machine on every run. It pays the full cost of the clone and the checkout each time, and nobody reuses a warm working tree. A job that builds one service in a large monorepo needs only a few directories. Fetching and writing everything else wastes minutes.
Three options reduce that cost, and they work together:
--depth 1limits history to the latest commit.--filter=blob:noneskips file contents until Git needs them.--sparsewithgit sparse-checkout setlimits the working tree.
A minimal CI checkout
This is the clone from the workflow chapter, written as a script that stops on the first error.
#!/usr/bin/env bash
set -eu
git clone --depth 1 --filter=blob:none --sparse \
https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api libs/common--sparse starts with only the root files checked out. The set command then fetches the blobs for the two directories and writes them to disk. Everything else is never downloaded.
Using it in a hosted CI system
Many CI products expose these options directly. In GitHub Actions, actions/checkout accepts a sparse-checkout input and a filter input:
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
filter: blob:none
sparse-checkout: |
services/api
libs/common
sparse-checkout-cone-mode: trueCheck your CI vendor's documentation for the exact input names. If the vendor has no such options, run the script above in a plain shell step.
Choosing directories automatically
Hard-coding directories works for a single-purpose job. Pipelines that cover many services usually derive the list from a file in the repository, such as a per-service dependency list. Keep the list under version control so it changes with the code.
#!/usr/bin/env bash
set -eu
service="$1"
git sparse-checkout set "services/$service" libs/commonAlways quote variables. A service name that contains a space or a glob character should not change what Git receives.
Common errors in automation
| Symptom | Cause | Fix |
|---|---|---|
| Build cannot find a shared library | The directory is outside the sparse set | Add it with `git sparse-checkout add` or list it in the setup step |
| `git diff origin/main...HEAD` fails or shows too much | A depth-1 clone has no merge base | Deepen with `git fetch --deepen=50`, or use a larger `--depth` for jobs that compare branches |
| Job is slower than expected | Blobs are fetched one at a time on demand | Set the full directory list up front so Git fetches them in one batch |
| Tool scans the whole repository and reports missing files | The tool expects the full tree | Point it at the sparse directories, or skip sparse checkout for that job |
| Lint or ownership checks pass locally but fail in CI | The local tree has more files than the CI tree | Run the CI setup script locally to reproduce |
Two of these deserve extra care.
Shallow history and change detection
Many pipelines decide what to build by running git diff --name-only against the base branch. A --depth 1 clone cannot do this, because the base commit is missing. Either fetch the base commit explicitly, or compute the change list in an earlier job that has full history and pass it to the sparse jobs.
Lazy fetching needs network access
With a blobless clone, any command that reads a missing file contents triggers a fetch from the remote. That includes git log -p and git blame. If the job later loses network access or credentials, these commands fail in confusing ways. Fetch everything you need while the connection is still available.
Practical rules
- Prefer cone mode in CI. It is simpler to reason about and works with the sparse index.
- Keep the directory list in one place, not scattered across job definitions.
- Make the setup step a script you can run on a developer machine.
- Add a clear failure message when a required directory is missing, so errors point to the sparse set and not to a vague build failure.
Exercise
A CI job clones with --depth 1 --filter=blob:none --sparse, then runs git diff --name-only origin/main...HEAD to find changed services. The command fails. Why, and what are two ways to fix it?
Show answer
A depth-1 clone contains only the latest commit, so Git cannot find the merge base with origin/main. Fix it by fetching more history (for example git fetch --deepen=50 or a larger --depth), or by computing the list of changed files in an earlier job that has full history and passing it to the sparse job.