Sub topic 10
Sparse checkout for working on part of a tree
This chapter assumes you know basic Git (clone, commit, branch, status) and can use a shell. It also assumes you know that a repository stores commits, trees, and blobs. Earlier chapters covered shallow and partial clones. Both reduce what Git downloads. Sparse checkout is different: it reduces what Git writes into your working directory.
What sparse checkout does
A normal checkout writes every tracked file of the current commit into your working tree. In a monorepo that can mean hundreds of thousands of files. Most of them are irrelevant if you only work on one service.
Sparse checkout tells Git to keep only a chosen subset of paths in the working tree. The rest of the tree stays in the repository, but the files are not written to disk.
Two facts matter for the rest of the chapter:
- Sparse checkout does not change what is downloaded or stored in
.git. In a plain clone, all objects are still fetched. It saves disk space in the working tree, and it makes commands that scan the working tree faster, such asgit status. - The index (Git's staging area) still lists every tracked file. Files outside your selection are marked with the
skip-worktreebit. Git treats that bit as "pretend this file is unchanged and don't look for it on disk."
The download savings come only when you pair sparse checkout with a partial clone. That combination is the subject of the next chapter. Here the focus is on how sparse checkout itself works.
Starting a sparse checkout
The simplest way to begin is to ask for a sparse checkout at clone time:
git clone --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api docs
git sparse-checkout listThe --sparse flag makes the initial checkout contain only the files at the top level of the repository. The set command then replaces the selection with services/api and docs. The list command prints the directories currently selected.
You can also turn sparse checkout on in an existing repository. Running git sparse-checkout set <dirs> enables it if it is not already on.
git sparse-checkout set services/api docsRun git status afterward. Git reports how much of the tree is present, with a line such as "You are in a sparse checkout with 4% of tracked files present."
Cone mode
Git has two pattern styles. Cone mode is the one to use. Non-cone mode uses full gitignore-style patterns, is slower on large trees, and Git documents it as deprecated. Since Git 2.37, git sparse-checkout set uses cone mode by default. On older versions, you can request it explicitly:
git sparse-checkout set --cone services/api docsIn cone mode you list directories, not arbitrary patterns. Git then includes:
- Every file directly in the repository root.
- Every file directly inside each parent directory of a selected directory.
- Everything, recursively, inside each selected directory.
For services/api and docs, the result looks like this:
| Path | Present? | Reason |
|---|---|---|
| `README.md` (root) | Yes | Root files are always included |
| `services/Makefile` | Yes | Direct child of a parent of a selected directory |
| `services/api/src/main.py` | Yes | Inside a selected directory |
| `docs/guide/intro.md` | Yes | Inside a selected directory |
| `services/web/src/app.js` | No | Sibling directory, not selected |
| `tools/lint/config.yaml` | No | Unrelated directory |
The parent-file rule surprises people. A shared services/Makefile or services/package.json shows up even though you did not ask for it. This is intentional, because build files in parent directories often affect the directories below them.
Changing the selection
Use set to replace the whole selection and add to extend it:
git sparse-checkout add services/billing
git sparse-checkout set services/api docs tools/lintGit updates the working tree right away. It writes newly included files and removes files that fell out of the selection. Directories that contain untracked files are not removed, so Git does not delete work it does not know about.
To read directories from a file or another command, use --stdin:
printf '%s\n' services/api docs | git sparse-checkout set --stdinRemember that set replaces the selection. If you run git sparse-checkout set docs after the previous command, services/api leaves your working tree.
Turning it off
git sparse-checkout disableThis restores every tracked file to the working tree. The saved patterns stay in the repository's configuration. A later git sparse-checkout set ... starts a fresh selection.
How Git stores the selection
You rarely need to touch these files, but knowing them helps with debugging:
core.sparseCheckoutis a config flag that turns the feature on.core.sparseCheckoutConeis a config flag that selects cone mode..git/info/sparse-checkoutholds the patterns. In cone mode Git writes them for you in a fixed, structured form. Do not hand-edit them. Use thesparse-checkoutcommand.
Check the current state with:
git config --get core.sparseCheckout
git config --get core.sparseCheckoutCone
git sparse-checkout listEveryday behavior and edge cases
Most Git commands work as usual, but a few behave in ways that are worth knowing.
Switching branches. When you run git switch other-branch, Git applies your sparse patterns to the new commit. A directory you selected that does not exist on the other branch is simply empty. Files that exist only outside your selection are not written.
Merges and rebases with conflicts outside the cone. If a merge conflicts in a file outside your selection, Git writes that file into the working tree so you can resolve it. After you finish, run git sparse-checkout reapply to remove files that are out of scope again.
Editing a file outside the selection. If you force-create a file outside the cone, Git warns you that it is out of scope. Git may leave it in place, but it will not track changes to it the way it does for included paths. Add the directory to the selection instead.
Searching. git grep searches only the files in the working tree by default. To search the whole commit, name the revision: git grep pattern HEAD. In a partial clone, that can trigger downloads of the blobs it needs.
Large indexes. The index still contains an entry for every tracked file, so commands that read it do work proportional to the whole repository. Git 2.37 added the sparse index, which collapses excluded directories into single entries. Enable it with:
git sparse-checkout set --sparse-index services/api docsSome older tools, such as certain IDE integrations and libraries built on libgit2, do not understand the sparse index. If one of them misbehaves, switch it off with --no-sparse-index.
Common errors
- A directory you expect is missing. Run
git sparse-checkout list. In cone mode, a path that is not listed, and is not a parent of a listed path, is absent. - Extra files at the top of a parent directory. This is the parent-file rule. It is not a bug.
- A directory will not go away after you remove it from the selection. It probably contains untracked or ignored files, such as build output. Delete those, then run
git sparse-checkout reapply. - An error about patterns when you pass a file path. Cone mode accepts directories only. To include one file from a directory, you need non-cone mode, which Git discourages, or you should select its directory.
- The command is not found.
git sparse-checkoutrequires Git 2.25 or newer. Check withgit --version.
Exercise
You cloned a monorepo with git clone --sparse and ran git sparse-checkout set services/api. You then notice that services/Makefile is in your working tree, although you never selected it. Later you run git sparse-checkout set docs. Which of these paths are in your working tree afterward: README.md, services/Makefile, services/api/src/main.py, docs/intro.md? Explain why.
Show answer
README.md and docs/intro.md are present. README.md is a root file, and cone mode always includes root files. docs/intro.md is inside the selected directory docs. services/Makefile is absent. It appeared earlier only because services was a parent of the selected services/api. Once set replaced the selection with docs, services was no longer a parent of anything selected. services/api/src/main.py is absent as well, because set replaces the old selection instead of adding to it. Use git sparse-checkout add docs if you want to keep services/api.