Sub topic 6
Partial clone: skipping file contents you do not need
This chapter assumes you know the difference between repository history and the working tree, and that you can run git sparse-checkout set in cone mode. Sparse checkout shrinks what Git writes to disk. A plain clone still downloads every file version in the history. Partial clone fixes that part.
What a partial clone omits
Git stores four kinds of objects: commits, trees (directory listings), blobs (file contents), and tags. In a large monorepo, blobs make up most of the data. Every version of every file ever committed is a blob.
A partial clone asks the server to leave some objects out. The most common filter is blob:none:
git clone --filter=blob:none https://example.com/org/monorepo.gitYou get all commits and all trees, so git log, branch listing, and directory structure work as usual. You get no blobs yet. Git downloads a blob only when a command needs it, such as a checkout that writes the file to disk.
The remote you cloned from becomes a promisor remote. It "promises" to supply any missing object on request. Git records this in your config:
git config remote.origin.promisor
git config remote.origin.partialclonefilterThe first command prints true. The second prints blob:none.
Choosing a filter
| Filter | What is skipped | Typical use |
|---|---|---|
| `blob:none` | All blobs | Developer machines combined with sparse checkout |
| `blob:limit=1m` | Blobs of 1 MiB or larger | Repos with a few large binary files |
| `tree:0` | All trees and blobs | CI jobs that need only commit data |
blob:none is the usual pairing with sparse checkout. tree:0 saves more, but each directory listing then needs a fetch. Commands that walk history can become slow with it.
Combining it with sparse checkout
Without --sparse, a blob:none clone still checks out the whole working tree, so Git immediately fetches every blob for the tip commit. That defeats the filter. Adding --sparse limits the initial checkout to the top-level files:
git clone --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api libs/commonThe sequence of events:
Git batches the blob requests for a checkout into one fetch, so setting the sparse directories costs one round trip rather than one per file.
Lazy fetching afterward
Later commands can trigger more downloads:
git checkoutorgit switchto a branch that changes files inside your sparse set fetches the new blobs.git log -p,git blame, andgit diffbetween old commits fetch the blobs they compare, even for files outside the sparse set.git sparse-checkout addfetches the blobs for the new directory.
To see which objects you do not have locally, run:
git rev-list --objects --all --missing=print | grep -c '^?'Lines starting with ? are missing objects. This count is typically large right after a blob:none clone.
Common errors
- The server ignores the filter. The server must allow filtering (
uploadpack.allowFilter). Major hosts enable it. On a self-hosted server that does not, Git warns that the filter was ignored and sends everything. - Offline work fails. A command that needs a missing blob fails when it cannot reach the remote. Fetch what you need before you disconnect.
- History commands are unexpectedly slow.
git log -pover a long range triggers many on-demand fetches. Limit the range with a path or a commit count.
Exercise
You run git clone --filter=blob:none https://example.com/org/monorepo.git without --sparse. The clone downloads far more data than you expected. Why, and how do you fix it?
Show answer
Without --sparse, the clone checks out the full working tree, so Git fetches every blob for the tip commit right after the object-light clone. Add --sparse to the clone command, then choose directories with git sparse-checkout set. The initial checkout then covers only the top-level files, and Git fetches blobs for the chosen directories.