Sub topic 3
Sparse checkout in one command
This chapter assumes you can clone, commit, branch, and read git status, and that you are comfortable in a terminal. You should also know roughly what a monorepo is: one repository holding many projects.
The idea
A normal checkout writes every tracked file of the current commit into your working tree. In a large monorepo that can mean hundreds of thousands of files, most of which you never touch.
Sparse checkout changes only the working tree. Git still stores the full history and every commit. It writes to disk only the paths you select. The other files stay in the repository, and they come back when you ask for them.
Turning it on
The command you need is git sparse-checkout set. It enables sparse checkout if it is off, stores the paths you give it, and updates the working tree to match.
git clone --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api libs/commonThe clone uses two options that matter here:
--sparsestarts the clone with a minimal working tree. Only the files in the repository's top-level directory are checked out.--filter=blob:noneskips downloading file contents until Git needs them. A later chapter covers it in detail. For now, treat it as a way to make the clone faster.
The set command then picks two directories. Git fetches their files and checks them out. Everything else disappears from the working tree.
What you see afterwards
After the command, ls shows the top-level files and the two directories you chose. Directories such as services/billing are gone from disk.
The commit you are on has not changed. git log still shows the full history. git status does not report the missing files as deleted, because Git knows they were left out on purpose.
flowchart TDThe diagram above is not used. The flow is shown in the diagram below.
Changing your selection
set replaces the previous selection. If you run it again with different paths, the old directories leave your working tree and the new ones appear.
git sparse-checkout set services/api services/billingGit removes libs/common from disk in this case, because it is no longer in the list. Files you have modified in a directory that is being removed can block the update. Commit or stash those changes first.
Why this speeds things up
Several everyday operations scale with the number of files in the working tree:
- Checking out a branch rewrites files that differ between commits. Fewer files on disk means less to write.
git statusscans the working tree for changes.- Editors, file watchers, and build tools index every file they can see.
Sparse checkout reduces all three. It does not reduce the size of the .git directory unless you also use a partial clone, which is why the two features are usually combined.
Checking that it is on
Run this to confirm the state:
git config core.sparseCheckout
git sparse-checkout listThe first command prints true when sparse checkout is active. The second lists the directories you selected. Both are covered more fully in later chapters.
Common mistakes
- Running
setand expecting it to add.setreplaces the list. Usegit sparse-checkout addto extend it. - Passing a file path. In the default mode, you select directories, not single files. Use the directory that contains the file.
- Leaving uncommitted edits in a directory you drop. Git may refuse to update. Save your work first.
- Assuming the build still works. If your project depends on a directory outside the sparse set, the build will fail. Add the dependency to the set.
Exercise
You ran git sparse-checkout set services/api libs/common. Later you run git sparse-checkout set services/billing. Which directories are in your working tree now, and does git log show less history?
Show answer
Only services/billing (plus the top-level files) is in the working tree, because set replaces the earlier selection. git log shows the same full history, since sparse checkout only affects the working tree.