Sub topic 7
Cloning a Monorepo Sparsely from the Start
This chapter shows how to combine partial clone and sparse checkout so that a first clone of a large monorepo downloads and writes only what you need. It assumes you can clone, commit, branch, and read git status. It also assumes you know what a monorepo is and have seen cone mode and git sparse-checkout set in earlier chapters.
The problem with enabling sparse checkout after cloning
If you run a plain git clone and then enable sparse checkout, you have already paid the cost you wanted to avoid. Git has downloaded every commit, tree, and file version in the history. It has also written every file of the default branch to disk. Sparse checkout then removes most of those files from the working tree, but the downloaded data stays in .git.
Two separate costs are involved:
- Object transfer and storage. The commits, trees, and blobs (file contents) that Git downloads and keeps in
.git. - Working tree size. The files written to disk and tracked by the index.
Sparse checkout only addresses the second cost. To address the first, you combine it with a partial clone. The goal is to set both up before Git does the expensive work, which means doing it at clone time.
The two flags
A sparse clone uses two options on git clone.
--filter=blob:nonemakes the clone partial. Git downloads commits and trees, but not blobs. Blobs are fetched later, on demand.--sparsemakes the initial checkout sparse. Git starts with a sparse checkout enabled in cone mode, so only the files directly in the repository root are checked out.
The full command looks like this:
git clone --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api libs/commonThe clone downloads all commits and all trees, so the full directory structure and history are known. It checks out only the top-level files. The git sparse-checkout set command then selects services/api and libs/common. Git sees that it needs the blobs for files in those directories, requests them from the remote in a batch, and writes them to the working tree.
What each step transfers
| Step | Commits and trees | Blobs | Working tree |
|---|---|---|---|
| `git clone --filter=blob:none --sparse` | All | Only those needed for the root files | Root files only |
| `git sparse-checkout set services/api libs/common` | Already present | Blobs for the two directories at the current commit | Root files plus the two directories |
| Later `git log -p` over old history | Already present | Blobs fetched as needed | Unchanged |
The remote that Git fetches from is called the promisor remote. A promisor remote "promises" to supply any object that Git is missing. Git records this in the repository configuration when you clone with a filter.
The following diagram shows the order of operations.
Why `--sparse` matters
Without --sparse, a clone with --filter=blob:none still performs a full checkout. Git must write every file in the default branch to disk, so it fetches every blob at that commit. You would get the history savings but lose the checkout savings, because the blobs for the whole tree come down anyway.
With --sparse, the initial checkout is limited to the root files, so very little is fetched. This is why the two flags are used together.
If you want to know the exact state after the clone, check it:
git sparse-checkout list
git config core.sparseCheckout
git config extensions.partialCloneThe first command lists the current directory set, which is empty right after --sparse. The second prints true. The third prints the name of the promisor remote, usually origin.
Choosing directories before the first checkout
There is a short window after the clone in which you have a sparse checkout with only the root files. If you want to avoid even that, you can clone without checking out and then set the directories before the first checkout:
git clone --filter=blob:none --no-checkout https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set --cone services/api libs/common
git checkout mainHere --no-checkout skips the initial checkout entirely. The set --cone command turns on sparse checkout in cone mode and records the directories. The final git checkout main populates the working tree with only the chosen directories plus root files. Use this form when the root of the monorepo holds many large files, or when you want a single batch of blob requests.
The two forms give the same end state. The --sparse form is shorter and is the usual choice. Replace main with your default branch name if it differs.
Reducing history as well
Partial clone avoids blobs, but you still download every commit and tree. In a monorepo with a long history, the trees alone can be large, because each commit has a tree for each changed directory level. If you only need recent history, add --depth:
git clone --depth 1 --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/apiA shallow clone with --depth 1 fetches only the latest commit and its trees. Combined with the blob filter and a sparse checkout, this is the smallest practical starting point. It suits automated jobs that build one commit and never inspect history.
A shallow clone has a cost. Commands that walk history, such as git log, git blame, and git merge-base, see only the commits you have. You can deepen the clone later with git fetch --deepen=50 or convert it to a full history with git fetch --unshallow.
The `--filter=tree:0` alternative
Git also supports --filter=tree:0, which skips both blobs and trees. It gives the smallest possible clone, but every operation that needs a tree must contact the server. That makes git log with paths, git blame, and git checkout slow, and it generates many small requests. For interactive use, blob:none is the better trade-off. Treat tree:0 as an option for single-purpose automation.
What happens when you touch missing objects
With a blob-less clone, some commands trigger network requests later:
git checkoutof another branch or commit fetches blobs for the sparse directories at that commit.git log -pandgit diffbetween old commits fetch the blobs they need, one batch per command.git blameon a file fetches the versions of that file in its history.git sparse-checkout addfetches blobs for the newly added directory.
These fetches are automatic, and they fail if you are offline. When you plan to work without a network connection, run the commands you need beforehand, or fetch the needed blobs explicitly:
git fetch --filter=blob:none origin
git sparse-checkout add services/billingThe first line refreshes commits and trees without blobs. The second adds a directory, which downloads its blobs now, while you are online.
Checking how much you saved
You can compare the cost of a sparse clone with a full one by inspecting the object database:
git count-objects -vH
git rev-list --objects --all --missing=print | grep -c '^?'The first command reports the size of the local object store. The second lists every object reachable from any ref and prints missing ones with a leading ?. Counting those lines gives you the number of objects not yet downloaded.
Common errors in a sparse clone
| Symptom | Likely cause | Fix |
|---|---|---|
| The clone is slow and large despite `--sparse` | `--filter=blob:none` was left out | Re-clone with both flags |
| The clone downloads many blobs at once | `--sparse` was left out, so Git did a full checkout | Re-clone with both flags, or run `git sparse-checkout set` afterwards and accept the already downloaded blobs |
| `fatal: filtering not recognized by server` | The server does not support partial clone | Use a server or hosting plan that does, or fall back to `--depth` plus sparse checkout |
| `git log` is slow on first use | Git is fetching blobs or trees on demand | Run it once while online, or use `--depth` and deepen when needed |
| Commands fail offline | Required blobs were never downloaded | Fetch them while online by adding the directory or checking out the commit |
| A directory you chose is empty | The path was wrong, or you used a file path in cone mode | Check with `git ls-tree -d HEAD path/` and use directory paths only |
If the server does not support filters, the --filter option produces a warning or an error depending on the server and Git version. In that case the sparse checkout still limits the working tree, but you download every blob.
Putting it together
A practical routine for a developer joining a monorepo:
- Clone with
--filter=blob:none --sparse. - Select the directories your team owns with
git sparse-checkout set. - Add shared libraries as the build tells you they are needed, using
git sparse-checkout add. - Keep the default history depth so that
git logandgit blamework.
For automation, the same routine applies with --depth 1 added.
Exercise
A teammate runs git clone --filter=blob:none https://example.com/org/monorepo.git and then git sparse-checkout set --cone services/api. They report that the clone still took a long time and downloaded far more data than expected. What did they leave out, and why did it matter?
Show answer
They left out --sparse. Without it, the clone performs a full checkout of the default branch, so Git fetches the blobs for every file at that commit before sparse checkout is enabled. The later git sparse-checkout set only shrinks the working tree. It cannot undo the download. Cloning with --filter=blob:none --sparse limits the initial checkout to the root files, so only the blobs for the chosen directories are fetched later.
Exercise
You need a build job that compiles only services/api at the latest commit and never reads history. Write the clone and sparse-checkout commands that transfer the least data, and name one command that would behave poorly in that clone.
Show answer
git clone --depth 1 --filter=blob:none --sparse https://example.com/org/monorepo.git, then cd monorepo, then git sparse-checkout set services/api. Commands that walk history, such as git log, git blame, or git merge-base, would behave poorly because the shallow clone holds only one commit. You can deepen it with git fetch --deepen=N or git fetch --unshallow.