git sparse-checkout Command in CI
git sparse-checkout populates the working tree with only the directories you list.
In a large monorepo, a CI job often needs just one package. Combined with a partial clone, sparse-checkout keeps the working tree (and disk and time) small.
Common flags
init --cone- enable sparse checkout in cone mode (directory-based, fast)set <dir>...- restrict the working tree to the given directoriesadd <dir>- add more directories to the sparse setlist- show the current sparse patternsdisable- turn sparse checkout off and restore the full treereapply- re-evaluate patterns against the index
Example
shell
# Check out only one service from a monorepo
git clone --no-checkout --filter=blob:none \
https://github.com/owner/monorepo.git repo
cd repo
git sparse-checkout init --cone
git sparse-checkout set services/billing
git checkout mainIn CI
Pair --filter=blob:none with sparse-checkout in cone mode to clone a monorepo quickly and materialize only the package a job builds. Cone mode is much faster than the pattern-matching default and is the recommended choice for CI.
Key takeaways
- sparse-checkout in cone mode materializes only the directories a CI job needs.
- Combine it with --filter=blob:none for a fast, small monorepo checkout.
- Cone mode outperforms the legacy pattern mode and is preferred in CI.
Related guides
git clone Command: Clone Repos in CIgit clone copies a remote repository into a new directory. Reference for --depth, --branch, --filter, and --r…
git checkout Command: Switch Refs in CIgit checkout switches branches or restores files. Reference for checking out a branch, --detach, and -b to cr…
git ls-files Command: List Tracked Files in CIgit ls-files lists files in the index. Reference for listing tracked, ignored, or modified files to drive lin…
git fetch Command: Update Refs in CIgit fetch downloads objects and refs from a remote without merging. Reference for --depth, --unshallow, --tag…