How partial clones work
I covered how a full git clone works under the hood in part 1 and part 2.
However, for large repos a full clone is slow. For example, a full clone of Git's own repository is 311 MB in size and took 16 seconds to download.
The newest commit (commit, tree, blob) is only 13 MB. The rest is history. In total the clone holds 86,000 commits, 170,000 trees and 167,000 blobs, every version of every file back to 7 April 2005.
Git lets you skip older objects to reduce the size of your clone. In this post, we will look at the three ways to do that and when to use them.
Three ways to do a partial clone
Here's our tiny repo: four commits, 13 objects. Commits point to trees, and trees point to blobs.
A shallow clone (--depth=1) gets the newest commit and its files: c4, its tree and its two blobs.
A blobless clone (--filter=blob:none) gets every commit and every tree, and no blobs.
A treeless clone (--filter=tree:0) gets only the commits.
| clone | tiny repo | Git's repo |
|---|---|---|
| full | 13 objects | 311 MB |
| blobless | 8 | 110 MB |
| treeless | 4 | 34 MB |
| shallow | 4 | 13 MB |
The three ways differ in how much history you get.
Shallow keeps no history. For your machine, the history starts at c4.
Treeless keeps the list of commits: who, when, the message, the parent. It can't tell what changed.
Blobless keeps every tree too. A tree lists each file's name and the hash of its contents. Compare c3's tree with c4's: the readme has the same hash, app.py doesn't. So git knows c4 changed app.py without opening either file.
How the server builds the pack file
Before the client asks "what do you have?", it says which version of the protocol it speaks. The server replies with what it can do.
If you run GIT_TRACE_PACKET=1 git clone, you should see output like this:
shallow and filter are the two that matter for partial clones. If a server doesn't list filter, git prints warning: filtering not recognized by server, ignoring and clones everything.
A full clone says want c4, done.
Blobless adds filter blob:none.
Shallow adds deepen 1, and the server answers shallow c4: your history stops there.
Blobless and treeless
In part 1, the server found c4's objects by reading its bitmap. That's a row with one box per object in the server's stored pack, and a 1 means c4 needs it.
The same bitmap file has one more row per kind of object. It marks which positions are commits, which are trees and which are blobs.
For a blobless clone, the server takes c4's row and crosses out every blob. 8 objects are left. It can do this without having to walk the history graph. It copies them byte for byte from the stored pack, like a full clone. Treeless crosses out the trees too.
On Git's repo, 256,880 of the 257,323 objects came straight from the stored pack:
Shallow: build it by hand
A shallow clone can't use bitmaps. c4's row means c4 and everything before it. No row means c4 alone.
So the server opens c4, opens its tree, collects the files and stops. Then it builds a new pack, one object at a time:
Counting looks up each object. Compressing is the delta search from part 2. pack-reused 0 means nothing was copied.
On a local copy of Git's repo that took about half a second, because there's so little to send. The server redoes it for every shallow clone.
After the clone
Checkout
Git clone does one more thing that parts 1 and 2 skipped. It writes the newest commit's files into your folder.
In a blobless clone, git opens c4, then c4's tree, and looks for the two files' hashes. Neither is there. Git asks the server for them by hash. Step through it:
On Git's repo, checkout fetched 4,758 blobs in one trip. It never downloaded the 162,700 older ones. You only download an old version when you open it.
Treeless is the same, one level up. Git can't know the files' hashes until it has the tree. So you need two trips (first to fetch the tree and then the blobs).
Comparing the different types of clones
Let's compare the following use cases against the different types of clones (full, treeless, blobless, shallow):
- Cloning a repo
- Searching the codebase
- Listing recent commits
- Reading one file's history
- Blaming a few lines
- Commit & push
Searching and pushing work the same for all 4 types of clones. History is where the four differ.
History of one file
Full and blobless are instant, because the trees say which commits changed the file. Treeless has no trees, so it fetches them one commit at a time.
Shallow is instant and wrong. It says the last change to lib/application.js was a dependency bump that never touched it:
That's the commit where history was cut, so every file looks like it was added there. The full clone shows four real changes, starting with 90ec6206, "Improve error logging by logging full error object".
Blame
Blame needs every old version of the file, and it only learns which one it needs next after reading the last. So it asks one at a time. -L 1,30 doesn't help, because it limits what's printed, not what git reads.
Shallow puts every line on one commit:
When to use which
Working in a repo yourself: blobless. You get all of history without any of the old files.
CI: shallow clone. Or treeless, if the build needs the commit list.
Coding agent: blobless. Agents read history quite often.