Sub topic 1
What a Clone Actually Downloads: Commits, Trees, and Blobs
A clone is slow when it moves a lot of objects. To make it faster, you need to know which objects Git moves and why. This chapter covers the three object types that make up a repository, how they link together, and what a normal git clone asks the server to send. The later chapters on shallow clones, partial clones, and sparse checkout all work by leaving some of these objects out.
Prerequisites
This chapter assumes you can:
- Run
git clone,git fetch,git commit, andgit logwithout help. - Use a terminal and read short shell commands.
- Accept that Git stores history as commits, trees, and blobs. You don't need to know how they connect yet, because this chapter explains it.
Git 2.30 or newer is recommended for the commands below. Most of them work on older versions.
The three object types
Git keeps everything in an object database inside .git/objects. Each object has an ID, which is a hash of its content, and a type. Three types matter for cloning.
| Type | What it holds | Typical size | How many you get |
|---|---|---|---|
| Commit | Author, date, message, pointer to one root tree, pointers to parent commits | Hundreds of bytes | One per commit |
| Tree | A directory listing: names, file modes, and the IDs of blobs or subtrees | Hundreds of bytes to a few KB | One per changed directory per commit |
| Blob | The raw bytes of one file version, with no name | Anything from empty to many MB | One per distinct file content |
A blob has no file name. The name lives in the tree that points to it. Two files with identical content, even in different directories, share one blob.
There is a fourth type, the annotated tag object. It matters less for cloning, and a later chapter covers tags.
How the objects link together
A commit points to exactly one tree, the root directory of the project at that moment. That tree points to blobs for files and to other trees for subdirectories. A commit also points to its parent commits, which forms the history chain.
The diagram shows why history is cheap when little changes. Commit B reuses the README blob because that file didn't change. It needs a new src tree and a new root tree because their contents changed. Changing one file creates one new blob and a new tree for every directory on the path from the file up to the root, plus a new commit.
See the objects yourself
The following script builds a tiny repository and inspects it. It runs as shown in a POSIX shell.
git init demo
cd demo
mkdir src
echo "hello" > README.md
echo "print('a')" > src/app.py
git add .
git -c user.name=Demo -c [email protected] commit -m "first"
echo "print('b')" > src/app.py
git add .
git -c user.name=Demo -c [email protected] commit -m "second"
git cat-file -p HEAD
git cat-file -p 'HEAD^{tree}'
git ls-tree -r HEAD~1
git ls-tree -r HEAD
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize)' --batch-all-objectsWhat each command shows:
git cat-file -p HEADprints the latest commit. You will see atreeline, aparentline, and the message.git cat-file -p 'HEAD^{tree}'prints the root tree. You will see ablobentry forREADME.mdand atreeentry forsrc.git ls-tree -r HEAD~1andgit ls-tree -r HEADlist every file with its blob ID. TheREADME.mdID is the same in both. Thesrc/app.pyID differs.- The last command lists every object in the database with its type and size. For this repository you should see nine objects: 2 commits, 4 trees, and 3 blobs. The four trees are the root and
srctrees of each commit, and the three blobs are the README and the two versions ofapp.py.
What a clone asks the server for
When you run git clone <url>, Git does roughly this:
- It connects and asks the server which refs exist. Refs are named pointers such as
refs/heads/mainandrefs/tags/v1.0. - It tells the server which refs it wants. For a default clone, that means all branches and all tags.
- It tells the server what it already has. In a fresh clone it has nothing.
- The server works out every object reachable from the wanted refs, minus anything you already have. It packs them into a single packfile and sends it.
- Git writes the pack to
.git/objects/pack, sets up remote-tracking branches, and checks out the default branch into the working tree.
"Reachable" is the key word. Starting from each wanted commit, Git walks to the commit's tree, every subtree and blob below it, and then to every parent commit and repeats. A default clone wants all history, so the walk covers every commit ever made on any branch or tag, along with each commit's trees and blobs.
You can mimic the server's walk locally:
git rev-list --objects --all | wc -lThis lists every object reachable from all refs and counts them. On the demo repository it prints 9. On a large repository it can print millions.
Where the bytes go
Packfiles shrink the transfer. The server stores similar objects as deltas, meaning "this object is that other object plus these edits". Compression and deltas make a long history much smaller than the sum of its file versions. Even so, the transfer still includes the following.
- Blobs usually dominate size. Source code is small, but repositories that hold images, binaries, generated files, or large data files can have blobs that dwarf everything else. A binary file edited 200 times can mean 200 large blobs, and binary data often delta-compresses poorly.
- Trees dominate object count in deep, wide repositories. Every commit that touches a file rewrites the trees along its path. A monorepo with long history can have many millions of trees.
- Commits are the smallest part. Even a million commits is usually only a few hundred megabytes before compression, and often less after.
You can measure the sizes in your own clone:
git count-objects -vHThe output reports loose objects, packed objects, and the size of the pack directory. Run it right after a fresh clone, when almost everything is in one pack, to see what the clone really cost on disk.
To see which objects are the largest, use this:
git rev-list --objects --all |
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' |
sort -k3 -n -r |
head -n 10The %(rest) field carries the path that rev-list attaches to each blob, so you can tell which files are big. Sizes here are uncompressed, so they overstate what travels over the network, but the ranking is still useful.
Checkout is a separate cost
A clone does two jobs: it downloads the object database, and it writes a working tree from one commit. These costs are independent.
- The download scales with history: every commit, tree, and blob reachable from the wanted refs.
- The checkout scales with the size of one snapshot: every file in the tree of the checked-out commit.
A repository can have a small current snapshot and a huge history. It can also have a modest history and a huge snapshot. The techniques in the coming chapters target different halves of this split. Shallow clones cut the commits you walk. Partial clones cut blobs or trees from the download. Sparse checkout cuts what is written to the working tree. Knowing which half is slow in your case tells you which tool to reach for.
Why this matters for the rest of the tutorial
Each technique removes a different kind of object or reachability walk:
| Technique | Removes from the download | Chapter focus |
|---|---|---|
| `--single-branch`, `--no-tags` | Commits, trees, and blobs reachable only from other refs | Fewer refs |
| `--depth N` | Commits older than N, plus objects only they reach | Less history |
| `--filter=blob:none` | All blobs, until needed | Defer file contents |
| `--filter=tree:0` | Trees and blobs, until needed | Defer almost everything |
| Sparse checkout | Nothing downloaded by itself, but fewer files written and fewer blobs needed when combined with a filter | Fewer paths |
Each row has a cost. When you leave an object out, a later command may need it and must either fail, work around its absence, or fetch it from the server. Understanding the object graph lets you predict which commands will do that.
Exercise
You make a commit that edits only docs/guide.md, a file two directories deep (docs/guide.md, with docs at the repository root). Which new objects does that commit create, and which existing objects does it reuse?
Show answer
It creates one new blob (the new content of guide.md), one new tree for docs, one new root tree (because the docs entry changed), and one new commit. Every other blob and every other subtree is unchanged, so the new root tree points to the same IDs as before for them. The new commit points to the previous commit as its parent.
Exercise
A full clone is slow, and git count-objects -vH shows a 4 GB pack, most of it from a few large binary files that were edited many times. Does the checked-out snapshot need to be large for the clone to be slow? Explain.
Show answer
No. The download scales with all reachable history, so many old versions of the binary files make the pack large even if the current snapshot is small. The checkout cost depends only on the files in the one checked-out commit.