Git Reference ยท Staging and Committing
git sparse-checkout command
Reference for git sparse-checkout: init, set, add, list, reapply and disable, cone mode versus non-cone patterns, and how it combines with partial clone.
git sparse-checkout limits which files appear in your working tree. The repository still contains the full history and every file, but only the directories you select are written to disk. In a large monorepo this saves disk space and makes git status and checkouts faster.
The default and recommended mode is cone mode: you list directories, and Git includes everything inside them plus the files directly in the repository root and in the parent directories of each selected directory. Non-cone mode accepts gitignore-style patterns and is more flexible but slower and deprecated for new use.
git sparse-checkout set src/api docs
git sparse-checkout list
git sparse-checkout disable
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
git sparse-checkout init [--cone | --no-cone] [--sparse-index] git sparse-checkout set [--cone | --no-cone] [--stdin] <dir-or-pattern>... git sparse-checkout add <dir-or-pattern>... git sparse-checkout list git sparse-checkout reapply git sparse-checkout disable git sparse-checkout check-rules git sparse-checkout clean
| Name | Description |
|---|---|
init |
Enable sparse checkout (in cone mode, with only the root files). |
set |
Replace the selection with the given directories or patterns and update the working tree. Enables sparse checkout if needed. |
add |
Add to the existing selection. |
list |
Print the current directories (cone) or patterns (non-cone). |
reapply |
Re-apply the rules, for example after a merge brought back excluded files. |
disable |
Turn it off and restore the full tree. |
--cone / --no-cone |
Choose directory-based or pattern-based mode. |
--sparse-index |
Store the index in a compact form that collapses excluded directories. |
--stdin |
Read the list from standard input. |
Returns: no output on success. Errors: in cone mode, file patterns such as *.c are rejected with an error telling you to use --no-cone; set refuses to run if untracked or modified files would be lost.
Usage notes
- Cone mode includes more than you name. Files in the repository root and files directly inside each parent of a selected directory always appear, so
README.mdis present above. - Rules live in
.git/info/sparse-checkout. Thecore.sparseCheckoutsetting (andcore.sparseCheckoutCone) records whether it is active; see sparse-checkout file. - Excluded files are not deleted from Git, only from disk. They carry the skip-worktree flag (
Singit ls-files -v). - History is still complete. Combine with a partial clone (
git clone --filter=blob:none --sparse) to avoid downloading file contents you will never open. - Local changes are protected. If an excluded path has modifications, Git keeps it on disk until it is clean.
- Switching branches re-applies the rules, so a different branch may show different files if its directories differ.
- Other tools (editors, build systems) will not see missing directories; keep that in mind for cross-package builds.
git clone --sparsestarts with only the root files already selected.