Sub topic 7
Cloning a Monorepo Sparsely from the Start
This chapter assumes you know basic Git (clone, commit, branch, status), can use a command line, and have read the earlier chapters on cone mode and partial clone. Here you combine those pieces into one workflow that avoids downloading and writing files you do not need.
Why combine the two features
Sparse checkout and partial clone solve different halves of the problem.
- Sparse checkout limits which files Git writes to your working tree.
- Partial clone with
--filter=blob:nonelimits which file contents (blobs) Git downloads.
A plain git clone of a large monorepo downloads every blob and writes every file. If you run git sparse-checkout set afterward, the working tree shrinks, but you have already paid for the full download. Starting sparse and partial avoids that cost.
The clone command
git clone --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api libs/commonEach part has a job:
--filter=blob:noneasks the server to send commits and trees but no blobs. Git fetches blobs later, only when it needs them.--sparseturns on sparse checkout during the clone. The initial checkout contains only the files directly in the repository root, so Git does not fetch blobs for the whole tree.git sparse-checkout set services/api libs/commonselects the two directories you work in. Git now fetches the blobs for the files in those directories, in a batch, and writes them to disk.
Recent Git versions use cone mode by default for git sparse-checkout set. In cone mode, you name directories, and Git includes the files in each directory you name, everything beneath it, and the files directly inside each of its parent directories.
What happens during the process
The repository still knows the full tree structure. You can run git log, list tree contents, and switch branches. The missing blobs are fetched on demand from the server, which Git calls the promisor remote.
Checking the result
After the clone, confirm what you have:
git sparse-checkout list
git ls-files | head
git count-objects -vgit sparse-checkout list prints the directories in your set. git ls-files lists files in the index, which can include files outside your set, because the index still tracks them. Look at the directory on disk to see what Git actually wrote.
Shallow history for one-off clones
If you do not need history, such as in a build job, add --depth 1:
git clone --depth 1 --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/apiThis fetches only the latest commit, no blobs, and then checks out one directory. It is the smallest practical clone. The cost is that git log, git blame, and merge-base calculations have little or no history to work with.
Common errors
- Forgetting
--sparse. With only--filter=blob:none, Git still checks out the whole tree. It then fetches every blob right away, which defeats the purpose. - Running offline after cloning. Blobs outside what you have fetched are missing. Commands that need them, such as
git checkoutof another branch orgit diffagainst old commits, fail without network access to the server. - Expecting
setto be additive.git sparse-checkout setreplaces the current set. Usegit sparse-checkout addto extend it. - Using a server without filter support. If the server does not support partial clone, Git warns that the filter is ignored and sends everything.
Exercise
You run git clone --filter=blob:none https://example.com/org/monorepo.git and the clone still takes a long time and fills your disk with files from every directory. Which option is missing, and what command then selects the directories you want?
Show answer
The --sparse option is missing. Without it, Git checks out the full working tree and fetches all the blobs it needs to do so. Clone with --sparse, then run git sparse-checkout set <dir1> <dir2> to choose the directories.
Exercise
Why might you avoid --depth 1 on your own development clone, even though it makes the clone smaller?
Show answer
It truncates history, so commands like git log, git blame, and merge-base lookups for branches have little data to work with. For development, use --filter=blob:none and keep full history. Reserve --depth 1 for CI or one-off builds.