Sub topic 7
Partial Clones: Downloading Objects on Demand
This chapter assumes you know basic Git use (clone, fetch, commit, branch), are comfortable on the command line, and know that Git stores commits, trees, and blobs. A commit records a snapshot and its parents. A tree is a directory listing. A blob is the content of one file version.
The idea
A shallow clone cuts history. A partial clone keeps full history and leaves out some objects. Git downloads the missing objects later, when a command needs them. You pass a filter to git clone, and the server leaves out the objects that match it.
Blobless clone
The most common filter is blob:none. It downloads all commits and trees, but no 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.mdgit log with a path only needs commits and trees, so it works without file contents. The clone also checks out HEAD, so Git fetches the blobs for the current files during the clone. git show HEAD:README.md reads a blob. If that blob is not on disk, Git requests it from the remote.
Treeless clone
The tree:0 filter omits trees and blobs. Only commits arrive up front. Combined with a shallow depth, it suits a build that needs only the current snapshot.
git clone --filter=tree:0 --depth 1 https://example.com/org/big-repo.gitChoosing a filter
| Filter | Downloaded up front | Good for |
|---|---|---|
| `blob:none` | Commits and trees | Daily development with full history |
| `tree:0` | Commits only | Build jobs, usually with `--depth 1` |
A third filter, blob:limit=<size>, skips only blobs above a size, for example --filter=blob:limit=1m. It helps when a few large binary files cause most of the weight.
How lazy fetching works
The remote you cloned from becomes a promisor remote. It promises to supply any object the local repository lacks. Git records this in the repository config. When a command needs a missing object, Git contacts the promisor and fetches it.
You can check whether a repository is a partial clone and which filter it used:
git config --get remote.origin.promisor
git config --get remote.origin.partialclonefilterThe first command prints true for a partial clone. The second prints the filter, such as blob:none.
Common errors and costs
- Offline surprises. Commands that need a missing object fail without a network connection. Examples are
git diffacross old commits,git blame, and checking out an old branch. - Many small fetches. A command that touches many missing blobs one at a time can be slower than a full clone. Git batches requests in many cases, but a treeless clone doing deep history work can trigger a large number of fetches.
- Server rejects the filter. The server must support filtering. If it does not, the clone fails or warns that the filter is ignored. Server support is covered in a later chapter.
Exercise
You run git clone --filter=blob:none on a large repository, then disconnect from the network. Which works: git log --oneline or git show v1.0:src/main.c for a file you have never opened? Why?
Show answer
git log --oneline works because commits are stored locally. git show v1.0:src/main.c likely fails because the blob for that file version was never downloaded, and Git cannot ask the promisor remote while offline.