Sub topic 11
Combining Partial Clone and Sparse Checkout in a Monorepo
This chapter assumes you know how to clone, fetch, and check out in Git. It also assumes you have met the --filter=blob:none option and know that Git stores commits, trees, and blobs. Here you will combine a partial clone with sparse checkout so that a large monorepo costs you little in both download size and disk space.
Why the two features belong together
A partial clone and a sparse checkout limit different things.
- A partial clone limits which objects Git downloads. With
--filter=blob:none, Git skips file contents until a command needs them. - A sparse checkout limits which files Git writes into your working tree.
Used alone, a blobless clone still downloads every blob the first time you check out a branch, because a full checkout needs every file. Used alone, sparse checkout still downloads the entire object database. Together, Git writes only the files you selected, so it fetches only those blobs.
Create the clone
Run the clone with both options. The --sparse flag starts the working tree with only the files in the repository root.
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 set command switches to cone mode and checks out the two directories. In cone mode you name directories, not arbitrary patterns. Git always keeps the files directly in the repository root, which is why the first checkout was not empty. Because those blobs are missing locally, Git fetches them in one batch from the remote, called the promisor remote, and then writes the files.
Cone mode matters for speed. Git can match whole directories with simple prefix checks. It does not need to test every path against a list of patterns.
Change your selection later
You can widen or narrow the selection at any time.
git sparse-checkout add libs/shared
git sparse-checkout listThe add command appends a directory to the current set. Git fetches the blobs for libs/shared when you run it. Narrowing with set removes files from disk, but their blobs usually stay in the object database. Selecting them again is then fast and needs no network.
Check what happened
Confirm the setup with the commands from the spine:
git config --get remote.origin.promisor
git config --get remote.origin.partialclonefilter
git sparse-checkout listYou should see true, blob:none, and your directory list. If the first two print nothing, the clone was not partial, which usually means the server ignored the filter.
Common errors
- Commands outside your paths trigger fetches.
git log -p,git grepagainst old revisions, andgit blameon a file outside the sparse set all need blobs you do not have. Git fetches them on demand, one request per command. Limit these commands with a path, or add the directory first. - Forgetting
--sparse. Without it, the clone checks out every file and downloads every blob for the current commit. The filter then saves only history blobs. - Expecting a smaller object database for tree data. Sparse checkout does not remove trees. Git still downloads every commit and tree, so a very large history still costs time.
Exercise
You cloned with git clone --filter=blob:none --sparse, then ran git sparse-checkout set services/api. A teammate asks why git log -p -- services/billing is slow the first time. What is happening, and how can you avoid it?
Show answer
The history of services/billing needs old blobs, and those blobs were never downloaded because the filter skipped them and services/billing is not in the sparse set. Git fetches them from the promisor remote on demand while the command runs. To avoid it, add the directory with git sparse-checkout add services/billing, which fetches the current blobs in one batch, or accept a single slow first run and let Git cache the results.