Sub topic 8
Choosing a filter: blob:none, blob:limit, and tree:0
This chapter assumes you know how to clone, fetch, and commit. It also assumes you know that Git stores commits, trees (directory listings), and blobs (file contents). A partial clone uses a filter to leave some of those objects out of the download. You choose the filter with --filter=<spec>. Which objects you leave out decides what stays fast and what triggers a network fetch later.
What each filter omits
A filter names the object types, or the object sizes, that the server should not send.
| Filter | Commits | Trees | Blobs |
|---|---|---|---|
| none (full clone) | all | all | all |
| `blob:none` | all | all | none |
| `blob:limit=<n>` | all | all | only those smaller than `<n>` |
| `tree:0` | all | none | none |
Git fetches any omitted object later, on demand, from the server it came from. The chapter on promisor remotes covers how that works. Here, the useful question is which commands will need an omitted object.
blob:none: the blobless clone
blob:none keeps the full commit graph and every directory listing. It skips file contents.
git clone --filter=blob:none https://example.com/org/big-repo.git
cd big-repo
git log --oneline -- README.md
git show HEAD:README.mdThe clone still checks out the default branch, so Git downloads the blobs for that one snapshot in a single batch. Everything after that depends on the command:
git log,git log -- path,git branch, andgit merge-basework offline. They need only commits and trees.git log -p,git blame,git diffbetween old commits, andgit showof an old file need blobs. Git fetches them as it goes.git checkoutof another branch fetches the blobs that differ from what you already have.
Blobless is the usual choice for daily development. Your history is complete and searchable by path. You pay for file contents only when you look at them.
blob:limit: skipping only large files
blob:limit=<n> omits blobs of at least n bytes. It accepts the suffixes k, m, and g.
git clone --filter=blob:limit=1m https://example.com/org/big-repo.gitThis clone downloads every small source file and skips large binaries such as images, archives, and datasets. It helps when a repository is mostly ordinary code and a few huge assets, and when you want most commands to work offline.
Two details matter:
- Checkout still needs the files in your working tree. If a large file exists at
HEAD, Git fetches it during the initial checkout. The limit saves bandwidth for old versions of large files, not for the current ones. - The limit applies to individual blobs. A repository of ten thousand 500 KB files is unaffected by
blob:limit=1m.
If the large files at HEAD are the problem, combine the filter with sparse checkout. Sparse checkout is covered later in this tutorial.
tree:0: the treeless clone
tree:0 downloads commits and nothing else. It also skips the trees and blobs that a blobless clone would still download.
git clone --filter=tree:0 --depth 1 https://example.com/org/big-repo.gitThe initial checkout still needs the trees and blobs for one commit, so Git fetches those. Because the clone is also shallow, little else is fetched. This is the smallest practical starting point for a build that needs only the current files.
A treeless clone without --depth is riskier. Any command that has to walk trees across history triggers many fetches:
git log -- pathmust compare trees in each commit to find where a path changed.git blameandgit diffacross old commits need the trees at both ends.git checkoutof an older revision needs that revision's full tree.
Git fetches missing trees in batches, but a long history can still produce many round trips. Treeless is a good fit for a one-shot job. It is a poor fit for a clone you will use for weeks.
tree:\<depth\>: a related spec
tree:<depth> omits trees at or below the given depth. tree:0 is the case that omits all of them. Values above 0 are rarely used. Know that the syntax exists, but expect to use blob:none, blob:limit, and tree:0 in practice.
Seeing the cost for yourself
You can count the missing objects in a partial clone. The ? prefix marks an object that Git knows about but does not have.
git rev-list --objects --all --missing=print | grep -c '^?'
git count-objects -vHRun these right after the clone and again after you run git log -p. The first number drops as Git fetches objects, and the size reported by count-objects grows.
You can also confirm that a clone used a filter:
git config --get remote.origin.promisor
git config --get remote.origin.partialclonefilterThe first prints true. The second prints the filter you used, for example blob:none.
A decision path
Comparing filters with other options
A shallow clone cuts history and keeps full file content for the commits it has. A filter keeps history and cuts content or structure. They solve different problems:
- A shallow clone is smallest, but commands that need old commits fail or give wrong results.
- A blobless clone keeps full history semantics. Commands that need old content pay with network fetches, and they fail if the server is unreachable.
- A treeless clone sits between them. It keeps the commit graph but pays heavily for path-based history queries.
--depth 1 with tree:0 or blob:none combines both savings. Use it only when you are sure the job never needs history.
Common errors
- Using
tree:0for a long-lived clone. Everyday commands become slow because each one fetches trees. Switch toblob:noneby cloning again, since changing the filter on an existing clone is error-prone. - Expecting
blob:limitto shrink the checkout. The files atHEADare fetched regardless of size. - Working offline. A partial clone cannot fetch omitted objects without the server. Run the commands you will need before you disconnect.
- The server rejects the filter. Some servers do not allow filters. Git may print a warning that the filter is not supported and then perform a full clone, or the clone may fail. The server-support chapter covers this.
- Assuming
git gcreclaims the savings. Fetched objects stay in the repository. A clone that has been used for a while can grow back toward full size.
Summary
blob:noneomits all file contents and keeps all commits and trees.blob:limit=<n>omits only blobs of at leastnbytes. Checkout still fetches the large files it needs.tree:0omits trees and blobs. It suits shallow, one-shot jobs and is costly for history queries.- Use
rev-list --missing=printand theremote.origin.*config keys to check what a clone has and how it was made.
Exercise
A CI job needs only the files at the tip of main to run a build. It never runs git log or git blame. Which clone command is most appropriate, and why would the same command be a poor choice for a developer's long-lived working copy?
Show answer
Use git clone --filter=tree:0 --depth 1 --single-branch --branch main <url>. It downloads one commit and only the trees and blobs the checkout needs. For a developer's long-lived copy, tree:0 makes history commands such as git log -- path and git blame fetch trees from the server on demand. That is slow and needs network access. --filter=blob:none is the better fit there, because it keeps all commits and trees locally.
Exercise
You clone with --filter=blob:limit=1m. A 40 MB video file exists at HEAD. Is it downloaded during the clone? What about a 40 MB version of the same file from three years ago?
Show answer
The 40 MB file at HEAD is downloaded, because the initial checkout needs it for the working tree. The old version is not downloaded during the clone. Git fetches it only if a command such as git show or git checkout of that old commit needs it.