Sub topic 2
Listing, Adding, and Disabling Sparse Directories
Assume you know clone, status, branches, and basic shell commands. A monorepo is a Git repository containing many projects. Sparse checkout limits which project directories appear in your working tree, the files on disk that you edit.
Manage the sparse set
After enabling sparse checkout, list the directories currently selected:
git sparse-checkout listAdd another directory without replacing the existing selection:
git sparse-checkout add services/billingThe add command updates the sparse set. Git then checks out the new directory and keeps the directories already selected.
Disable sparse checkout when you need the complete working tree:
git sparse-checkout disableThis restores tracked files to disk. It does not delete repository history or change your commits.
Partial clone: skip unneeded file contents
Sparse checkout controls files written to disk. A partial clone controls which Git objects are downloaded. With --filter=blob:none, Git initially skips blob objects, which contain file contents, and downloads them when a later operation needs them.
These features solve different costs and can be combined:
- Sparse checkout reduces working-tree files.
- A partial clone reduces initial object transfer.
- A normal clone can still use sparse checkout later.
Clone sparsely from the start
Use --sparse to begin with a sparse working tree. Add --filter=blob:none to avoid downloading file contents that the initial checkout does not need:
git clone --depth 1 --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api--depth 1 is optional. It creates a shallow clone containing only the latest commit, so it reduces history as well as file transfer. Use it in CI when older history is unnecessary. Avoid it when you need reliable history-based tools, such as git log across earlier commits.
Exercise
Which command adds services/billing while preserving the current sparse directories?
Show answer
git sparse-checkout add services/billing