Sub topic 7
Partial Clones: Downloading Objects on Demand
A shallow clone cuts the size of a repository by dropping old commits. A partial clone keeps every commit and delays downloading some of the objects those commits point to. This chapter explains what a partial clone is, how to create one, and what changes when you use one day to day.
This chapter assumes you can clone, fetch, commit, and branch. It also assumes you know that Git stores history as commits, trees (directory listings), and blobs (file contents). Filter choices and the internals of lazy fetching get their own chapters, so this one stays at the working-model level.
The idea: keep the graph, defer the contents
In a large repository, most of the bytes are blobs. Every version of every file that ever existed is a blob. Commits and trees are small by comparison. They only record who changed what, and which names point to which blobs.
A partial clone uses that split. You ask the server to leave out some objects, and Git downloads them later, only when a command needs them. The commit graph stays complete, so history commands that need only commits and trees still work.
The simplest form is the blobless clone:
git clone --filter=blob:none https://example.com/org/big-repo.git
cd big-repo
git log --oneline -- README.md
git show HEAD:README.mdThis clone downloads all commits and all trees. It does not download blobs during the transfer itself. Two things then happen:
git clonefinishes by checking out the default branch. The working tree needs file contents, so Git fetches the blobs for that one commit in a single batch.- Later commands that need other blobs fetch them from the remote when they run.
In the example above, git log --oneline -- README.md needs only commits and trees. Git compares tree entries to find commits that changed the file, so no blob download is needed. git show HEAD:README.md needs the file contents. That blob is already local from the checkout, so it also needs no download.
What the transfer looks like
The first command in a blobless clone talks to the server once for the graph and once for the checkout. Later commands may trigger more requests.
How a partial clone differs from a shallow clone
Both approaches reduce the first download, but they give up different things.
| Property | Full clone | Shallow clone (`--depth 1`) | Blobless clone (`--filter=blob:none`) |
|---|---|---|---|
| Commits downloaded | All | Latest only | All |
| Trees downloaded | All | Latest commit's | All |
| Blobs downloaded | All | Latest commit's | Current checkout's, then on demand |
| `git log` over full history | Works | Truncated | Works |
| Old file contents offline | Yes | No, not present | No, fetched when needed |
| Needs network after clone | No | No, for what exists | Yes, for old blobs |
A shallow clone cannot answer questions about commits it never downloaded. A partial clone knows all of those commits exist. It lacks only some of their contents, and it can get them from the server. For that reason, partial clones are usually the better choice for a developer's long-lived working copy. Shallow clones fit throwaway jobs better. Choosing between them is the subject of a later chapter.
What stays fast and what slows down
Whether a command triggers downloads depends on whether it reads blob contents.
Commands that usually need no extra downloads:
git log,git log --oneline, andgit log -- path(path filtering compares trees)git branch,git tag,git merge-basegit statusafter the first checkout, when the working tree matches HEADgit rev-listwithout--objectscontent inspection
Commands that fetch blobs when they run:
git checkoutorgit switchto a commit whose files you do not have yetgit show,git diff, orgit log -pacross old commitsgit blame, which reads file contents at many past commitsgit grepagainst a revision other than the working tree
Git batches the requests it knows about. git log -p over 200 commits may issue several fetches rather than 200. Even so, a command that touches a lot of history can be slower than the same command on a full clone. Run git blame on a long-lived file in a fresh blobless clone and you will see the delay. The first run pays the download cost. Later runs on the same file are fast because the blobs are now local.
Inspecting what you have and what is missing
A partial clone records that it is partial in its configuration:
git config --get remote.origin.promisor
git config --get remote.origin.partialclonefilterThe first command prints true. The second prints the filter you used, such as blob:none. The word promisor refers to the remote that has promised to supply any missing objects. A later chapter covers how that works.
To count objects that are referenced but not present locally, use --missing=print. Missing objects are printed with a leading ?:
git rev-list --objects --all --missing=print | grep -c '^?'On a fresh blobless clone this number is large, because most historical blobs are absent. Run it again after a git log -p over a range of commits and the count drops, because those blobs were fetched.
To see how much disk the repository uses, run:
git count-objects -vHCompare the output before and after commands that fetch blobs. This is the quickest way to find out which of your habits trigger downloads.
Working offline
A full clone can show any file at any commit with no network. A partial clone cannot, once a command needs a blob you do not have. Git then tries to contact the remote, and with no connection the command fails with an error about a missing object or a failed fetch.
If you plan to work offline, such as on a flight, do the following beforehand:
- Run the commands you expect to need while still connected, such as
git log -p -- path/you/care/about. - Or fetch everything with a full clone in another directory.
The blobs fetched in step 1 stay in the repository, so later runs work offline.
Server requirements
The server decides whether filters are allowed. The remote must support the filter feature in the Git protocol. Large hosting services support blob:none and related filters. A self-hosted server may need uploadpack.allowFilter set to true in its configuration. If the server does not support filtering, Git warns that the filter was ignored, or the clone fails, depending on the Git version and server. In the first case you silently get a full clone, so check the config values shown earlier after your first clone on a new host.
Getting the rest later
A partial clone is not permanent. Because the filter is stored in configuration, later fetches use the same filter. New fetches on a blobless clone bring in new commits and trees, but not their blobs, until a command needs them.
If you decide you want everything, you can fetch every missing object in one pass by running a command that reads them all, or by cloning again without a filter. For a repository with a long history this is the same cost as a full clone, so decide before you start whether you want to pay it.
Common mistakes
- Expecting offline history inspection.
git logworks offline, butgit log -pandgit blameneed blobs. - Assuming the filter worked. Check
remote.origin.partialclonefilterafter cloning from a new server. - Using a partial clone with a remote you cannot reach later. For example, a build agent that loses access to the Git host after the clone step cannot fetch missing blobs.
- Blaming the clone for slow commands. If one command is slow only once, it was fetching blobs. Repeat it to check.
Exercise
You run git clone --filter=blob:none https://example.com/org/big-repo.git and later run git log --oneline -- docs/guide.md and then git blame docs/guide.md. Which command is more likely to contact the remote, and why?
Show answer
git blame is more likely to contact the remote. git log -- path only compares commits and trees, which a blobless clone already has. git blame must read the contents of docs/guide.md at many past commits, and those blobs are missing until Git fetches them on demand. You can confirm this by running git rev-list --objects --all --missing=print | grep -c '^?' before and after. The count drops after the blame.
Exercise
Why is a blobless clone usually a better fit than --depth 1 for a developer's everyday working copy, and what is the cost of that choice?
Show answer
A blobless clone keeps the full commit graph, so git log, git merge-base, and branch comparisons work normally. A depth-1 clone has only the latest commit. The cost is that old file contents are not stored locally, so commands such as git blame or git log -p fetch blobs from the remote and need network access.