Sub topic 11
Combining Partial Clone and Sparse Checkout in a Monorepo
This chapter assumes you know what commits, trees, and blobs are. It also assumes you have read the earlier chapters on --filter=blob:none and promisor remotes, and that you can run git clone, git fetch, and git checkout comfortably.
Two features that solve different halves of the problem
A monorepo holds many projects in one repository. A developer on the API service rarely needs the mobile app, the data pipelines, or ten years of binary assets. Two Git features cut that cost, and they act on different layers.
| Feature | Layer it controls | What it saves |
|---|---|---|
| Partial clone (`--filter=blob:none`) | The object database in `.git` | Download size and disk used by file contents you never read |
| Sparse checkout | The working tree on disk | Files written to disk, plus the blobs Git must fetch to write them |
Used alone, each leaves a gap. A blobless clone still checks out every file at HEAD, so Git immediately downloads every blob for the current commit. A sparse checkout on a full clone still downloads every blob in history. Together, Git downloads all commits and trees, but only the blobs for the paths you selected.
The standard recipe
This is the combined form from the topic plan:
git clone --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api docs
git sparse-checkout listEach step does a specific job:
--filter=blob:nonemarks the clone as partial. Git records the origin as a promisor remote, meaning a remote that promises to supply missing objects later.--sparsestarts the clone with a sparse checkout already enabled. Only the files in the repository root are written to the working tree, so the initial checkout fetches few blobs.git sparse-checkout set services/api docsreplaces the selection with those two directories. Git updates the working tree and fetches the blobs it needs in one batch request.git sparse-checkout listprints the active directories so you can confirm the selection.
Because the missing blobs are requested in a single batch, set is much faster than the same checkout done by lazy fetching one file at a time.
Avoiding even the root files
If the repository root holds large files, you can skip the initial checkout entirely. Configure the sparse paths first, then check out:
git clone --filter=blob:none --no-checkout https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout init --cone
git sparse-checkout set services/api docs
git checkout mainReplace main with the repository's default branch. Git fetches blobs only for the selected paths and the root files that cone mode always includes.
Cone mode and what it includes
Cone mode is the default pattern style for git sparse-checkout set in current Git versions. You give it directory names, and it never needs pattern syntax. It follows these rules:
- Every file directly in the repository root is included.
- Every file directly inside each parent directory of a selected path is included. For
services/api, the files directly inservices/are present, but other subdirectories ofservices/are not. - Everything under a selected directory is included, recursively.
So set services/api docs gives you the root files, the files directly in services/, all of services/api/, and all of docs/. Directories such as services/billing/ stay absent.
Cone mode matters for performance as well as convenience. It lets Git match directories instead of testing every path against patterns. The older non-cone mode accepts gitignore-style patterns, but it is slower on large trees and is discouraged in the Git documentation.
Changing the selection later
You can widen or narrow the working tree at any time:
git sparse-checkout add services/billing
git sparse-checkout set services/api
git sparse-checkout disableaddkeeps the current directories and appends one. Git fetches blobs for the new directory only.setreplaces the whole list. Directories dropped from the list are removed from disk, but their blobs usually stay in.git, so re-adding them later is quick and needs no network.disablerestores the full working tree. On a blobless clone this triggers a download of every blob at the current commit, which can be as large as a full checkout. Run it only when you mean it.
What still reaches the network
Sparse checkout restricts the working tree. It does not stop commands from reading objects outside your paths. Anything that reads file contents from history can trigger a lazy fetch from the promisor remote, even for directories you did not check out.
| Command | Behavior in a blobless, sparse clone |
|---|---|
| `git log --oneline -- services/api` | Works from commits and trees only. No blob fetch. |
| `git log -p -- services/api` | Fetches the blobs needed to compute each diff, one batch per set of commits. |
| `git blame services/api/main.py` | Fetches old versions of that file across its history. Can be slow. |
| `git grep pattern` | Searches checked-out files only. No fetch. |
| `git grep pattern <rev>` | Reads blobs from that revision, which may include paths outside the sparse set, and can fetch many. |
| `git diff main...feature` | Fetches blobs for changed files in both revisions, including files outside your sparse paths. |
The rule of thumb: commands that only walk commits and trees are free. Commands that compare or search content fetch blobs, and they are not limited by your sparse paths.
Choosing the right shape for the job
A fully combined clone is not always the best option. Pick the shape by how you use the history:
- Daily development in one area of the monorepo:
--filter=blob:none --sparse. You keep full commit and tree history, sogit log -- pathand branch operations stay fast and offline. This is the main use case for this chapter. - Build or test job that needs one subtree at the current commit: add
--depth 1as well. A command such asgit clone --filter=blob:none --depth 1 --sparse ...fetches one commit and its trees, then the blobs for your paths. Shallow limits from earlier chapters apply, so history commands will not work. - Code review that must compare branches: prefer a blobless sparse clone over a shallow one. Shallow clones can lack the merge base, which breaks
git diff main...feature.
Diagnosing a combined clone
When a clone behaves unexpectedly, such as slow commands or surprise downloads, first confirm what kind of repository you have:
git config --get remote.origin.promisor
git config --get remote.origin.partialclonefilter
git sparse-checkout listThe first command prints true for a partial clone. The second prints the filter spec, for example blob:none. The third prints your selected directories. If sparse-checkout list prints nothing or an error, sparse checkout is not active and the whole tree is checked out.
To see which objects are absent locally, list them with the --missing=print option, which marks missing objects with a leading ?:
git rev-list --objects --all --missing=print | grep -c '^?'The count is the number of objects Git has promised but not downloaded. On a large repository the command itself walks all history and can take a while. To see when fetches happen, run a suspicious command with GIT_TRACE=1. Lines mentioning fetch and --filter show each on-demand request.
Common problems and fixes:
- A checkout is slow and prints "remote: Enumerating objects" repeatedly. Git is fetching blobs one batch at a time. Widen the sparse set once with
git sparse-checkout setinstead of switching directories in many steps. - A command fails offline with a missing object error. A blob outside your local set is needed. Reconnect, or restrict the command to your sparse paths.
- The server rejects the filter. Some servers do not allow filtered fetches. Git prints a warning that the filter was ignored, and you receive a full clone. Check the server's partial-clone support before relying on it.
- Disk use grows over time. Lazily fetched blobs stay in
.git. They are not removed when you narrow the sparse set. A fresh clone is the simplest way to reset.
Exercise
A teammate runs git clone --filter=blob:none --sparse <url>, then git sparse-checkout set services/api. Later they run git diff main...feature and the command pauses to download data from the remote, even though the feature branch touches only services/billing. Why does this happen, and which clone option would reduce the disk and network cost of the original clone for a build job that needs only the current files of services/api?
Show answer
Sparse checkout limits only the working tree, not what commands may read from the object database. git diff must compare file contents, so Git fetches the missing blobs for the changed services/billing files from the promisor remote. For a build job needing only the current files, add --depth 1 (a shallow clone), as in git clone --filter=blob:none --depth 1 --sparse <url>, so Git fetches one commit's trees instead of the full history.