Sub topic 10
Sparse checkout for working on part of a tree
This chapter assumes you know basic Git (clone, fetch, checkout, commit) and that a Git tree maps to a directory. Shallow and partial clones reduce what Git downloads. Sparse checkout reduces what Git writes into your working directory. It does not change what is in the repository.
What sparse checkout does
A normal checkout writes every tracked file into the working tree. In a monorepo that can mean hundreds of thousands of files, which slows down git status, editors, and build tools.
With sparse checkout, Git still tracks every path in the index, but it only materializes the paths you select. Files outside the selection are marked "skip-worktree" and do not appear on disk.
Cone mode
Git has two pattern styles. Cone mode is the default in current Git versions and the one to use. You list directories, and Git includes:
- every file directly inside the repository root,
- every file directly inside each parent directory of a selected directory,
- everything beneath each selected directory.
Cone mode matches whole directories, so Git can evaluate it quickly. The older non-cone mode accepts arbitrary gitignore-style patterns, but it is slower and deprecated.
Using it
You can enable sparse checkout in an existing clone, or start one with --sparse.
git clone --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api docs
git sparse-checkout listWith --sparse, the working tree starts with only the top-level files. The set command replaces the current selection with the directories you name.
To change the selection later, use these commands:
| Command | Effect |
|---|---|
| `git sparse-checkout set <dirs>` | Replace the selection with these directories |
| `git sparse-checkout add <dirs>` | Add directories to the current selection |
| `git sparse-checkout list` | Print the selected directories |
| `git sparse-checkout disable` | Restore the full working tree |
Git updates the working tree immediately. Directories you drop are removed from disk, unless they contain untracked or modified files, in which case Git warns and leaves them.
What it does not save
A plain --sparse clone still downloads every blob in history, because the objects arrive before checkout decides which files to write. You save disk space in the working tree and time in git status, but not clone time or object storage.
To reduce the download as well, combine sparse checkout with a partial clone. The next chapter covers that combination.
Common errors
- A file you expect is missing. It is probably outside your cone. Run
git sparse-checkout list, thengit sparse-checkout addthe directory. - Files appear that you did not select. In cone mode, files at the root and directly inside parent directories are always included. This is by design.
- Pattern syntax is rejected or behaves oddly. Cone mode takes directory paths, not globs. Use plain paths such as
services/api, notservices/*. - Commands outside the cone seem to ignore files.
git grepsearches only the checked-out files by default. Usegit grep <pattern> <commit>to search a committed tree.
Exercise
You cloned a monorepo with git clone --sparse, then ran git sparse-checkout set services/api. Later you need docs/ as well, without losing services/api. Which command do you run, and why not set?
Show answer
Run git sparse-checkout add docs. The set command replaces the whole selection, so git sparse-checkout set docs would drop services/api from the working tree. add appends to the existing selection.
Exercise
Does git clone --sparse alone make the clone download less data? Explain.
Show answer
No. It only limits which files are written to the working tree. Git still downloads all commits, trees, and blobs. To reduce the download, add a filter such as --filter=blob:none.