Sub topic 3
How Git Treats Files Outside the Sparse Set
Assume you know clone, status, branches, and basic command-line use. A monorepo is a Git repository containing many projects. Sparse checkout limits which files appear in your working tree, the files currently on disk.
Files outside the sparse set
Git keeps repository history and index entries for excluded files. It usually marks those index entries with the skip-worktree bit. Git then avoids writing those files to disk and avoids checking their working-tree state during normal commands.
The files are still available. Expanding the sparse set can restore them:
git sparse-checkout add services/billingDo not edit excluded files by manually creating them unless you understand the consequences. A later checkout, merge, or sparse-set change can replace or remove them.
Pattern mode for irregular layouts
Cone mode selects directory-shaped layouts and is the safer default. Pattern mode uses gitignore-style rules, so it can describe irregular selections, such as “everything except documentation.”
git sparse-checkout set --no-cone '/*' '!/docs/'Pattern rules can be powerful, but they are easier to misunderstand. Test the result with git sparse-checkout list and inspect the working tree before relying on complex rules.
> callout note > Sparse checkout controls files in the working tree. It does not remove commits, branches, or file contents already stored locally.
Sparse index for faster commands
A normal index records every path, even excluded ones. A sparse index can represent excluded directory regions as collapsed entries. Commands such as status and add then process fewer individual paths.
git sparse-checkout init --cone --sparse-index
git sparse-checkout set services/apiThe sparse index helps most in very large repositories. Some older tools expect a fully expanded index and may fail or expand it automatically.
Sparse checkout in CI
CI can avoid downloading and checking out unrelated files:
git clone --depth 1 --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api--filter=blob:none delays file contents until Git needs them. The build must still select every directory required by scripts, shared libraries, and generated files.
Limitations and common pitfalls
- Tools that scan the repository may report missing files as errors.
- Builds can fail when dependencies are outside the sparse set.
- Merge and checkout operations can change excluded paths when they become relevant.
- A sparse checkout is not a security boundary. Do not use it to hide sensitive files.
- Check the sparse configuration before debugging a missing-file problem.
Exercise
Why can a build fail in a sparse checkout even though the file exists in Git history?
Show answer
The file may be outside the sparse set, so Git has not written it to the working tree. Add its directory or adjust the patterns.