Sub topic 3
Sparse checkout in one command
Prerequisites
This chapter assumes you can clone a repository, commit, switch branches, and read git status output. It also assumes you are comfortable in a shell and know what a monorepo is: one repository holding many services and libraries. You need Git 2.35 or later. Git 2.37 and later make cone mode the default, and this chapter passes --cone explicitly so it works on both.
The idea in one sentence
Sparse checkout tells Git to write only some of the tracked files into your working tree. The commits, branches, and history stay complete.
The earlier chapters covered why a large monorepo is slow and what Git stores versus what it shows on disk. This chapter uses one command to put those ideas to work.
git sparse-checkout set --cone services/api libs/commonAfter this command, your working directory contains the files at the repository root, the files in services/api and libs/common, and little else. Other directories such as services/billing or docs disappear from disk.
What the command does
git sparse-checkout set does several things in order:
- It turns on sparse checkout by setting
core.sparseCheckouttotruein the repository configuration. - With
--cone, it also setscore.sparseCheckoutConetotrue. Cone mode treats your arguments as directories, not as arbitrary patterns. - It writes the rules that describe your selection to
.git/info/sparse-checkout. - It updates the working tree to match. Files that no longer match are removed from disk, and files that now match are written.
The rules file is where Git remembers your choice. The next chapter covers its format. For now, treat it as an implementation detail that git sparse-checkout manages for you.
A runnable example
The following script builds a small monorepo from scratch so you can see the effect without a real project. It creates a repository in /tmp/mono-demo, so run it somewhere you do not mind having a scratch directory.
set -e
rm -rf /tmp/mono-demo
git init -q -b main /tmp/mono-demo
cd /tmp/mono-demo
mkdir -p services/api services/billing libs/common docs
echo "monorepo" > README.md
echo "api code" > services/api/main.py
echo "billing code" > services/billing/main.py
echo "shared code" > libs/common/util.py
echo "guide" > docs/guide.md
git add .
git -c user.name=demo -c [email protected] commit -q -m "initial commit"
echo "--- before ---"
find . -path ./.git -prune -o -type f -print | sort
git sparse-checkout set --cone services/api libs/common
echo "--- after ---"
find . -path ./.git -prune -o -type f -print | sortBefore the set command, all five files exist. After it, you see this:
./README.md
./libs/common/util.py
./services/api/main.pyThree things happened:
README.mdstayed because cone mode always keeps the files at the repository root.services/apiandlibs/commonwere kept because you named them.services/billinganddocswere removed from disk. Nothing was deleted from the repository.
What did not change
Sparse checkout affects only the working tree. Everything else behaves as before. You can confirm this from inside the demo repository.
git status
git log --oneline
git show HEAD:docs/guide.md
git ls-files -tgit statusreports a clean tree. Git does not treat the missing files as deletions.git logshows the full history, including commits that touched only excluded directories.git show HEAD:docs/guide.mdprints the file contents, because the object is still in the repository. The repository still has every file even though the working tree does not.git ls-files -tlists every tracked path with a status tag. Paths outside your selection carry the tagS, for exampleS docs/guide.md. Paths on disk carryH.
The S tag is the skip-worktree bit. Git sets it on each index entry that the sparse rules exclude. When Git sees the bit, it does not compare that path with the disk, so the absent file is not reported as deleted. A later chapter covers this in more detail.
Working with the sparse tree day to day
Most commands work as usual inside the sparse tree:
git add,git commit, andgit pushact on the files you can see.git switchandgit checkoutchange branches. Your sparse selection persists across branches, so Git writes only the matching files for the new branch.git pullandgit mergebring in changes from everyone, including changes to directories you excluded. Git records those changes in history, but it does not write the excluded files to disk.
The saved selection is part of the repository's local configuration. It is not committed or pushed, so each clone has its own selection.
The table below compares a full checkout with a sparse one.
| Aspect | Full checkout | Sparse checkout (cone mode) |
|---|---|---|
| Files on disk | Every tracked file | Root files plus the chosen directories |
| Commit history | Complete | Complete |
| `git status` after a branch switch | Compares all files | Ignores paths marked skip-worktree |
| Disk use and write time | Grows with repository size | Grows with the selection |
| Where the selection is stored | Not applicable | `.git/info/sparse-checkout` and config |
| Shared with collaborators | Not applicable | No, it is local only |
Changing and undoing the selection
Running set again replaces the selection. It does not add to it.
git sparse-checkout set --cone services/billingAfter this command, services/api and libs/common are gone from disk and services/billing is present. To keep the earlier directories, list them all again. The add subcommand extends the selection instead.
git sparse-checkout add libs/commonTo go back to a full checkout, disable sparse checkout. This restores every file in the working tree.
git sparse-checkout disableDisabling is safe and reversible. Your selection is kept, so a plain git sparse-checkout reapply or a new set can bring sparse checkout back. The next chapters cover listing, adding, and disabling in more depth.
Edge cases to know about
Local changes in excluded directories
Suppose you edit docs/guide.md and then run git sparse-checkout set --cone services/api. Git does not silently discard your edit. It warns that the path is outside the sparse definition but not clean, and it leaves the file where it is. Commit or stash your work before you change the selection to avoid surprises.
Untracked and ignored files
Git only removes files it tracks. If an excluded directory holds untracked or ignored files, such as build output or a virtual environment, Git leaves those files and the directory that contains them. You may see a directory that "should" be gone. Delete these files yourself if you want them gone.
Files you create outside the selection
You can create a new file in an excluded directory. Git will treat it as untracked and show it in git status. It can behave oddly, so avoid it. If you need that directory, add it to the selection.
A selection that names a path that does not exist
In cone mode, naming a directory that does not exist at the current commit is not an error. Git records the rule, and the directory appears if a later commit or branch contains it. Check your spelling when nothing shows up, because a typo fails silently in the same way.
Paths are relative to the repository root
The arguments to set are paths from the top of the repository, using forward slashes. In newer versions of Git, running the command from a subdirectory may resolve paths differently. Run it from the root while you are learning.
Common errors
- "Nothing changed after
set." You may be on an older Git wheresetdid not enable cone mode. Rungit --version, upgrade if it is below 2.35, and pass--cone. - "My earlier directories vanished."
setreplaces the selection. Useaddto extend it. - "A tool cannot find a file." The file is in an excluded directory. Add the directory, or check whether the tool reads files outside the directories you need, such as a shared build configuration.
- "The files are still there after
disable." That is expected.disablerestores everything and does not remove files. - "I still downloaded the whole repository." Sparse checkout does not reduce what
git clonefetches. It only limits what is written to the working tree. Reducing the download is the job of partial clone, which a later chapter covers.
Exercises
Exercise
You ran git sparse-checkout set --cone services/api and then git sparse-checkout set --cone libs/common. Which of the two directories is on disk now, and which command would you have used to keep both?
Show answer
Only libs/common is on disk, because set replaces the previous selection. To keep both, run git sparse-checkout add libs/common after the first command, or run git sparse-checkout set --cone services/api libs/common.
Exercise
After sparse checkout hides docs/, git status reports a clean tree and does not show docs/guide.md as deleted. Which mechanism explains this, and how can you see it for one path?
Show answer
Git sets the skip-worktree bit on the index entry of each excluded path, so it does not compare that path with the disk. Run git ls-files -t to see the tag S in front of the path, for example S docs/guide.md.
Exercise
A teammate says sparse checkout will make git clone download less data. Is that correct for a plain git clone followed by git sparse-checkout set?
Show answer
No. A plain clone fetches the full history and all file contents. Sparse checkout then limits only what is written to the working tree. Reducing the download needs partial clone, for example --filter=blob:none.