Sub topic 6
Partial clone: skipping file contents you do not need
This chapter assumes you can clone, commit, branch and check status in Git. It also assumes you know what a working tree is, and that sparse checkout restricts the working tree to chosen directories. Here you learn the second half of the combination: partial clone, which restricts what Git downloads and stores in the repository itself.
Why sparse checkout alone is not enough
Sparse checkout controls which files Git writes to disk. It does not change what git clone downloads. A normal clone with --sparse still transfers every commit, every tree and every file version (blob) in the full history. In a large monorepo that transfer can be many gigabytes, and the object database on disk grows to match.
Partial clone fixes the download side. It lets the client tell the server: "Send me the history, but leave out some objects. I will ask for them later if I need them."
The two features act on different layers:
| Feature | What it limits | Where the saving appears |
|---|---|---|
| Sparse checkout | Files written to the working tree | Disk space for checked-out files, speed of `status` and `checkout` |
| Partial clone | Objects downloaded into `.git` | Clone time, network use, size of `.git` |
The three object types involved
A Git repository stores three kinds of objects that matter here.
- A commit is a snapshot record: author, message, parent commits, and a pointer to a root tree.
- A tree is a directory listing. Each entry points to a blob or to another tree.
- A blob is the content of one version of one file.
In a monorepo, blobs make up most of the bytes. Commits and trees are comparatively small. Partial clone exploits this by leaving out blobs, and in the extreme case trees as well.
Filter specifications
You request a partial clone with --filter=<spec>. The common specs are:
| Filter | Omits | Typical name |
|---|---|---|
| `blob:none` | All blobs | Blobless clone |
| `blob:limit=<size>` | Blobs of at least that size, such as `blob:limit=1m` | Size-limited clone |
| `tree:0` | All trees and blobs. Only commits arrive. | Treeless clone |
A blobless clone is the usual choice for sparse checkout. You get the full commit graph and every directory listing, so commands like git log, git branch and git merge-base work offline. File contents arrive only when something needs them.
git clone --filter=blob:none --no-checkout https://example.com/org/monorepo.git
cd monorepo
git config --get remote.origin.partialclonefilterThe --no-checkout flag stops Git from checking out the default branch right away. Without it, Git would fetch all blobs for the tip commit immediately, which defeats the purpose unless you are also using sparse checkout. The git config command prints blob:none, which confirms the filter is recorded.
What the clone records in your configuration
When you clone with a filter, Git writes three settings into .git/config:
remote.origin.promisor = truemarks the remote as a promisor remote.remote.origin.partialclonefilter = blob:nonestores the filter for later fetches.extensions.partialClone = origintells Git that this repository may have missing objects, and which remote to ask.
A promisor remote is a server that promises to supply any missing object on request. Git treats a missing object as expected, not as corruption. In a normal repository, a missing blob makes git fsck fail. In a partial clone, Git knows the blob is on the promisor remote and does not complain.
The setting extensions.partialClone is a repository format extension. Old Git versions that do not know it will refuse to operate on the repository rather than misinterpret it. If a build server uses an old Git, you will see an error about an unknown extension.
Lazy fetching
When a command needs an object that is missing locally, Git contacts the promisor remote and downloads it. This is lazy fetching. It happens automatically in the middle of the command.
Git batches these requests. During checkout it collects all the blob IDs it needs and fetches them together. Commands that touch objects one at a time can be much slower, because each missing object may trigger its own round trip.
Once fetched, a blob stays in your local object database. A second checkout of the same version costs nothing.
Commands that trigger lazy fetches
Any command that reads file contents can trigger a fetch:
git checkoutandgit switch, for files that will appear in the working tree.git diffbetween two commits, for the files that changed.git log -pandgit show, for the changed files in each commit shown.git blame, which walks the history of one file and reads many versions of it.git grepagainst a commit other than your working tree.
Commands that only read commits and trees stay local in a blobless clone. git log --oneline, git branch -a and git rev-parse are examples.
Seeing what is missing
Git can list the objects it does not have. The --missing=print option prints each missing ID with a leading ?.
git rev-list --objects --all --missing=print | grep '^?' | headOn a fresh blobless clone with --no-checkout, this lists very many IDs. After you check out some directories, the count drops by the number of blobs you fetched.
To compare disk use, check the object database size:
git count-objects -vHLook at size-pack. For a blobless clone of a large repository, it is usually a small fraction of a full clone.
Combining with sparse checkout
The two features work well together because sparse checkout tells Git exactly which blobs the working tree needs. With a blobless clone, only the blobs for your sparse directories are fetched at checkout. Blobs for all other directories and all older history stay on the server.
The next chapter walks through the complete workflow. The short form looks like this:
git clone --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api libs/commonHere --sparse starts with only the top-level files checked out, and git sparse-checkout set triggers a batched fetch for the blobs in the two chosen directories.
Without --sparse, a blobless clone still checks out the whole tip commit and fetches every current blob. You save the historical blobs but not the current ones.
Choosing a filter
Choose by how you will use the clone.
- Day-to-day development with sparse checkout. Use
blob:none. History commands such asgit logandgit merge-basestay fast and offline, and you fetch blobs for the directories you check out. - Build or test jobs that only need the latest files. Combine
--depth 1withblob:none, or usetree:0if you will fetch trees on demand. These jobs rarely need history. - A repository with a few huge binary files and mostly small text files. Use
blob:limit=<size>. Small files arrive with the clone and the large ones stay on the server until needed. The limit applies to the blobs you do not need to download at clone time. Large files that are part of the tip checkout are still fetched at checkout. - You want the smallest possible initial download and can tolerate slower history queries. Use
tree:0. Commit-only history is tiny, but commands likegit log -- path/to/fileandgit blamemust fetch trees, often one commit at a time, and can become very slow.
Treeless clones are a trade-off, not a free win. For long-lived developer clones, blobless is usually the safer choice.
Edge cases and common errors
The server must allow filtering. The server needs uploadpack.allowFilter enabled. Major hosting services enable it. On a self-hosted server it may be off, and the clone fails or silently ignores the filter, depending on the Git version. A warning such as "filtering not recognized by server, ignoring" means you got a full clone.
Local path clones ignore the filter. If you clone from a plain filesystem path, Git uses a shortcut and ignores --filter. Use a file:// URL when testing locally.
Offline work fails on missing blobs. If you are on a plane and run git checkout on a branch whose blobs you never fetched, Git cannot get them and reports an error that an object could not be fetched from the promisor remote. Fetch what you will need before going offline, for example by checking out the branches or directories you plan to use.
Removing the remote breaks the repository. The promisor remote is the only copy of the omitted objects. If the remote is deleted, renamed without updating configuration, or loses access, missing objects can never be recovered.
Garbage collection and history rewrite. Fetched blobs are ordinary local objects and can be pruned when unreachable, as usual. If a server permanently removes an object that you have not fetched, any command that needs it fails.
Authentication prompts mid-command. Because lazy fetching runs a network request inside commands like git diff, a credential prompt or token expiry can appear in unexpected places. In automation, make sure credentials stay valid for the whole job.
Unexpected slowness in blame and log -p. If a command seems to hang, it may be fetching objects one at a time. You can fetch ahead of time with a wider checkout, or use a narrower command, such as limiting git log to a path or a commit range.
Summary of the working model
Partial clone changes the rule "every object is local" into "every object is local or available from the promisor remote". Sparse checkout then makes the set of objects you really need small and predictable. Together they cut both the clone and the working tree to the part of the monorepo you work on, while the commit graph stays complete.
Exercise
You run git clone --filter=blob:none https://example.com/org/monorepo.git without --sparse or --no-checkout. The clone still downloads many gigabytes. Why, and what would you change to download much less?
Show answer
Without --sparse or --no-checkout, Git checks out the full tip commit after cloning, so it fetches every blob in the current tree. The filter only skips blobs from older history and from commits you have not checked out. Add --sparse (or --no-checkout) to the clone, then run git sparse-checkout set <dirs> so only the blobs for your chosen directories are fetched.
Exercise
In a blobless clone, which of these commands normally runs without contacting the server: git log --oneline, git blame README.md, git branch -a, git show HEAD~20? Explain.
Show answer
git log --oneline and git branch -a run locally, because they read only commits and refs, which a blobless clone contains in full. git blame README.md needs many versions of the file and git show HEAD~20 needs the changed blobs for that commit's diff. Both may trigger lazy fetches if those blobs are missing locally.