Sub topic 5
Listing, Adding, and Disabling Directories
This chapter covers the commands you use every day once sparse checkout is on: inspecting the current set of directories, changing it, and turning the feature off. It assumes you can use Git for cloning, committing, branching, and checking status. It also assumes you know what cone mode is from the previous chapter, where you pick whole directories and Git expands them into rules for you.
The sparse set is state you manage
The sparse set is the list of directories that Git materializes in your working tree. Git stores it in your repository's private directory, in .git/info/sparse-checkout. It is local to your clone. It is not committed, and it is not shared with teammates.
Three commands cover most of the management work:
| Command | What it does |
|---|---|
| `git sparse-checkout list` | Prints the current set |
| `git sparse-checkout add <dirs>` | Adds directories to the set |
| `git sparse-checkout disable` | Restores the full working tree |
There is also git sparse-checkout set, which replaces the whole set with the directories you give it. The difference between set and add is the most common source of confusion, so we return to it below.
Listing the current set
Start every session of sparse-checkout work by looking at what you have:
git sparse-checkout listIn cone mode, the output is one directory per line, relative to the repository root:
libs/common
services/apiThese are the directories you chose. Git also includes the files directly in the repository root, and the files directly inside each parent directory of a chosen one (here, the files directly in libs/ and services/). Those do not appear in the list, because they follow from the cone rules.
If sparse checkout is not enabled, the command exits with an error such as fatal: this worktree is not sparse. Newer Git versions print this message. Older versions may print nothing useful. Use git config core.sparseCheckout to see whether the feature is on. It prints true when enabled.
The list output is usable in scripts. For example, you can check whether a directory is already in the set:
git sparse-checkout list | grep -qx 'services/api' && echo "api is in the set"The -x flag makes grep match the whole line, so services/api does not match services/api-gateway.
Adding directories
Suppose you cloned with services/api and libs/common, and now a task needs services/billing. Add it:
git sparse-checkout add services/billingGit updates the stored rules and then writes the new files into your working tree. Everything already checked out stays where it is. You can add several directories in one call:
git sparse-checkout add services/billing libs/authIf the repository was cloned with a partial clone filter such as --filter=blob:none, add also downloads the file contents for the new directories from the remote. The command can take a while on a slow network and fails if you are offline. Local history operations still work, but a checkout of missing files needs a connection.
Add versus set
set replaces the set. add extends it. Compare the results, starting from the set libs/common and services/api:
git sparse-checkout set services/billing
git sparse-checkout listservices/billingThe two earlier directories are gone from the working tree. Git removes their files from disk, so any untracked or ignored files inside them stay, but tracked files disappear. This is safe for committed work, because the history is intact. It is not safe for uncommitted changes, which the next section covers.
To keep your earlier directories, either use add, or pass the full list to set:
git sparse-checkout set services/api libs/common services/billingUse set when you want to define the exact set, such as in a script. Use add when you are working by hand and only want to extend what you have.
What happens to uncommitted changes
When a directory leaves the set, Git has to remove its files from disk. Git will not discard your uncommitted edits to do so. If a tracked file in that directory has local modifications, the command stops with an error that names the file, and the set does not change. Commit or stash the changes first, or put the directory back into your set arguments.
Untracked files are a separate case. Git does not track them, so it leaves them in place. A directory that still holds untracked or ignored files, such as build output, stays on disk after it leaves the set. Git warns about this and does not delete it. You can remove those files yourself.
Cone mode input rules
In cone mode, you give directory paths, not patterns. A few rules apply:
- Paths are relative to the repository root, even if you run the command from a subdirectory. Older Git versions interpret them relative to the root as well, but check your version if results look wrong.
- A trailing slash is accepted and ignored:
services/api/andservices/apiare the same. - You cannot name a single file. Passing
README.mdin cone mode does not select that file. The root files are already included. - Wildcards are not supported. Use
--no-coneif you need them, which the pattern-mode chapter covers.
If you pass a path that does not exist in the current commit, Git accepts it. The rule is stored, and the directory appears if you later check out a commit that has it. This is useful when switching between branches that have different layouts, and it also means a typo fails silently. After a set or add, run git sparse-checkout list and look at the directories on disk to confirm that you got what you meant.
Reading from a file or standard input
For long lists, set and add accept the --stdin option. Git reads one path per line:
printf '%s\n' services/api libs/common libs/auth | git sparse-checkout set --stdinTeams often keep a list of directories per project in a text file and load it this way:
git sparse-checkout set --stdin < team-payments.txtSince the sparse set is local state, a shared file like this is how a team gets consistency without committing the .git file. Keep the list in the repository, or in a shared tooling repository, and apply it with one command.
Reapplying the rules
Sometimes the working tree and the rules disagree. This can happen after a merge, a rebase, or a checkout that brought in files outside the set. The reapply subcommand rewrites the working tree to match the current rules:
git sparse-checkout reapplyIt does not change the set. Use it when files that should be absent are present, or when a conflicted merge left entries that are no longer in the cone. Like set, it will not remove files that have uncommitted changes.
Disabling sparse checkout
To get the full working tree back:
git sparse-checkout disableGit writes every tracked file to disk and turns off sparse checkout. The stored rules in .git/info/sparse-checkout stay in place, so you can switch back without re-entering the list. To go back to your earlier set:
git sparse-checkout init --coneOn older versions, init re-enables the feature and reuses the stored patterns. In current Git versions, init is deprecated in favor of set, so a safer option is to save the list first:
git sparse-checkout list > my-sparse-set.txt
git sparse-checkout disable
# later
git sparse-checkout set --stdin < my-sparse-set.txtThe saved file works on any Git version that supports cone mode.
Disabling in a partial clone
Disabling has a cost if you used --filter=blob:none. The files outside your set were never downloaded. Turning off sparse checkout makes Git fetch all of them, which is the thing you avoided in the first place. On a large monorepo this can be many gigabytes and many minutes. Check the repository size with your hosting provider before you disable, and consider adding only the directories you need.
A short worked session
The following session shows the whole cycle. Assume you start with services/api and libs/common.
git sparse-checkout list
git sparse-checkout add services/billing
git sparse-checkout list
git sparse-checkout set services/billing libs/common
git sparse-checkout list
git sparse-checkout disable
git config core.sparseCheckoutThe expected results, in order:
- The first
listprintslibs/commonandservices/api. - After
add, the secondlistprintslibs/common,services/api, andservices/billing. - After
set, the thirdlistprintslibs/commonandservices/billing. Theservices/apifiles are removed from disk, provided they had no uncommitted changes. - After
disable, every tracked file is on disk, and the final command printsfalse.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `fatal: this worktree is not sparse` | Sparse checkout is off | Run `git sparse-checkout set <dirs>` |
| A directory vanished after `set` | `set` replaced the earlier list | Use `add`, or run `set` with the full list |
| `set` refuses and names a file | A tracked file in a removed directory has local changes | Commit or stash it, then retry |
| A new directory is missing after `add` | Typo in the path, which Git accepts silently | Check with `list` and fix the spelling |
| `add` hangs or fails in a partial clone | Git is fetching missing file contents | Check the network and the remote, then retry |
| Files appear that should be excluded | A merge or checkout brought them in | Run `git sparse-checkout reapply` |
Summary
listshows the directories you chose. It does not show the implied root and parent files.addextends the set.setreplaces it.- A directory can leave the set only if its tracked files are clean.
disablerestores the full tree. Save the list first if you want to return to it, and expect a large download in a partial clone.
Exercise
You are in a cone-mode sparse clone with the set services/api and libs/common. You run git sparse-checkout set services/billing and then notice that services/api is gone from disk. What command would have avoided this, and what command restores your original two directories plus billing?
Show answer
git sparse-checkout add services/billing would have kept the earlier directories, because add extends the set while set replaces it. To restore everything now, run git sparse-checkout set services/api libs/common services/billing, which defines the full set explicitly.
Exercise
A teammate wants to turn off sparse checkout on a clone made with --filter=blob:none because "it is only a quick check of one more directory". Why might git sparse-checkout disable be a poor choice, and what should they do instead?
Show answer
disable writes every tracked file to disk, so Git must download all the file contents that the partial clone skipped. That can be a very large download. Instead, they should run git sparse-checkout add <directory> for the one directory they need, which fetches only that directory's files.