Sub topic 5
Listing, adding, and disabling directories
This chapter assumes you can clone, commit, and branch in Git, and that you have already turned on sparse checkout in cone mode with git sparse-checkout set. Here you learn to manage the sparse set day to day: see what is in it, grow it, shrink it, and turn it off.
The sparse set is a list of directories
In cone mode, the sparse set is the list of directories whose files Git writes to your working tree. Files directly in the repository root are always included. Files in the parent directories of a chosen directory are also included. Everything else is left out of the working tree, though it stays in the repository history.
Because the set is stored in .git/info/sparse-checkout, it belongs to your local clone. It is not committed, and it does not change what teammates see.
Listing the current set
Run git sparse-checkout list to print the directories in the set.
git sparse-checkout set services/api libs/common
git sparse-checkout listThe output is one directory per line:
libs/common
services/apiIn cone mode, the list shows only the directories you chose, not the root files or parent directories that Git also includes. If sparse checkout is not enabled, the command prints an error telling you so.
Replacing versus adding
set and add look similar, but they differ in one important way.
| Command | Effect on the existing set |
|---|---|
| `git sparse-checkout set <dirs>` | Replaces the set with exactly the directories you name |
| `git sparse-checkout add <dirs>` | Keeps the current set and adds the directories you name |
A common mistake is running set when you meant add. Your earlier directories disappear from the working tree.
git sparse-checkout set services/api
git sparse-checkout add services/billing
git sparse-checkout listservices/api
services/billingIf you now ran git sparse-checkout set services/billing, the services/api directory would be removed from your working tree. Git deletes the files that are no longer in the set, so commit or stash any changes there first.
What happens when the set changes
When you add a directory, Git checks out its files. If you cloned with --filter=blob:none, Git first downloads the missing file contents from the remote, so the command can take a moment and needs network access. When you remove a directory from the set, Git deletes its files from disk. The commits that contain them are untouched.
Disabling sparse checkout
To get the full working tree back, run:
git sparse-checkout disableGit restores every file from the current commit. In a very large monorepo this can take a long time and use a lot of disk space, and in a partial clone it downloads every missing file. Make sure you have the space and bandwidth before you run it.
Disabling does not delete your sparse patterns. If you later run git sparse-checkout reapply, or enable sparse checkout again with set, Git applies the sparse rules to the working tree.
Common errors
- Wrong directory path. Paths are relative to the repository root and use forward slashes. A typo does not cause an error. Git simply adds a path that matches nothing, so the directory you wanted stays empty.
- Running commands from a subdirectory. Cone mode paths are still relative to the repository root, not your current directory. Check with
git sparse-checkout listafter a change. - Expecting a teammate's set to match yours. The set is local. Share the directory list in your team documentation or a setup script.
- Losing uncommitted work. Changes in a directory you remove from the set can block the command or be lost. Commit or stash first.
Exercise
You ran git sparse-checkout set services/api, then git sparse-checkout set libs/common. What is in your sparse set now, and which command should you have used for the second step to keep both directories?
Show answer
Only libs/common is in the set, because set replaces the existing set. To keep both directories you should have run git sparse-checkout add libs/common.
Exercise
Which command restores the complete working tree, and why should you think before running it in a large partial clone?
Show answer
git sparse-checkout disable restores every file. In a large partial clone it must download all missing file contents and write them to disk, which can take a long time and use a lot of space.