Sub topic 2
Why Large Repositories Are Slow to Clone
Prerequisites
This chapter assumes you can clone, fetch, branch, and commit with Git on the command line. It also assumes you know that Git stores history as commits, trees, and blobs. A commit points to a tree (a directory listing). A tree points to blobs (file contents) and to other trees. Here we use that model to explain why a plain git clone of a large repository takes minutes or hours. The later chapters cover the fixes.
What a full clone transfers
A default git clone copies the whole object database of the remote repository. That means every commit ever made, every tree each commit references, and every version of every file that any commit references. It then creates remote-tracking branches for every branch and downloads every tag. Last, it checks out one commit into your working directory.
Two sizes matter, and people often mix them up:
- The working tree is the set of files you see after checkout. It is one snapshot.
- The
.gitdirectory holds the whole history. In a long-lived repository it is often many times larger than the working tree.
Cloning cost follows the second size, not the first. A repository with a 200 MB working tree can have a 10 GB history.
Where the time goes
A clone is not just a download. Several stages run one after another, and each can be the slowest one.
The stages in detail:
- Ref advertisement. The server lists all its branches and tags. Repositories with tens of thousands of refs make this step noticeably slow, and it repeats on every fetch.
- Negotiation. The client sends
wantlines for the tips it needs. On a first clone it has no commits, so it sends nohavelines. The server therefore has nothing to exclude. - Object enumeration. The server walks the graph from every wanted tip to find all reachable commits, trees, and blobs. This walk is proportional to the number of objects, and it is why Git's progress output starts with "Enumerating objects" and "Counting objects".
- Compression. The server builds a pack file. Unless it can reuse deltas or packs it already has (for example from bitmap indexes), it spends CPU searching for similar objects to store as deltas. Hosting services often cache or precompute packs, but not for every request shape.
- Transfer. The pack streams over the network. This is the only stage that faster bandwidth fixes.
- Indexing. The client runs the equivalent of
git index-pack. It reads the entire pack, computes a hash for each object, and resolves every delta against its base. This is the "Resolving deltas" line in the progress output. It is CPU-bound and disk-bound on your machine. - Checkout. Git writes the files of one commit to disk. For a repository with hundreds of thousands of files this can take a while on slow filesystems, antivirus-scanned folders, or network drives.
Because of stages 3, 4, and 6, a fast network does not make a huge clone fast. A 1 Gbit/s link does not help if the server spends minutes compressing, or your laptop spends minutes resolving deltas.
What makes a repository large
Repositories grow along three different axes. They need different fixes, so it helps to know which one you have.
| Axis | What grows | Typical cause | Which later technique targets it |
|---|---|---|---|
| History depth | Number of commits and the trees they carry | Years of development, many contributors, automated commits | Shallow clone (`--depth`) |
| Tree breadth | Number of files and directories per snapshot | Monorepos with many services and libraries | Sparse checkout, `tree:0` filter |
| Blob weight | Bytes of file contents, including old versions | Binaries, generated files, vendored dependencies, data files committed once and later deleted | Partial clone (`--filter=blob:none`) |
Every commit has at least a root tree, and it also needs a new tree for each directory that changed on the path to a modified file. In a monorepo with deep directories, a one-line change can create five or six new tree objects. Tree objects are small, but there are a lot of them, and they are not skipped by a cheap trick like "download fewer files".
Blobs usually dominate size. Git stores each distinct file version once, and it delta-compresses similar versions, which works well for text. Compiled binaries, images, archives, and minified files delta poorly, so each version costs close to its full size.
Measuring before you optimize
Do not guess which axis hurts. Measure it in a full clone you already have, or in one you make once on a fast machine.
git count-objects -vH
git rev-list --count --all
git for-each-ref | wc -lcount-objects -vH reports pack sizes in readable units. rev-list --count --all counts commits reachable from any ref. for-each-ref | wc -l counts branches, tags, and other refs, which is the number the ref advertisement must carry.
To see how objects split by type, and how many bytes each type takes on disk, run this:
git rev-list --objects --all \
| git cat-file --batch-check='%(objecttype) %(objectsize:disk)' \
| awk '{ n[$1]++; s[$1] += $2 } END { for (t in n) printf "%-8s %9d objects %12d bytes\n", t, n[t], s[t] }'A typical result for an old, binary-heavy repository shows a few hundred thousand commit and tree objects but a blob line holding most of the bytes. If instead the commit count is in the millions and blobs are modest, history depth is your main cost. If the tree count is far higher than the commit count suggests, you have a wide monorepo.
%(objectsize:disk) reports the size after delta compression, which is close to what the pack stores and transfers. Raw uncompressed sizes are larger, and you can get them with %(objectsize).
To find the biggest individual blobs, sort the same stream:
git rev-list --objects --all \
| git cat-file --batch-check='%(objecttype) %(objectsize:disk) %(rest)' \
| awk '$1 == "blob"' \
| sort -k2 -n -r \
| head -n 10The %(rest) field carries the path that rev-list --objects printed next to each object. A path with only one hit can still be a past version that no longer exists in the current checkout.
Common misconceptions
"Deleting the file fixes it." Removing a large file in a new commit changes only the latest snapshot. Every earlier commit still references the blob, so a clone still downloads it. Only rewriting history removes it, and that is a disruptive change for everyone who has the repository.
"The working tree is small, so the clone should be fast." As shown above, the cost is in .git. A clone of a repository with a 50 MB checkout can still transfer gigabytes.
"It is a network problem." Compare the phases in the progress output. If "Receiving objects" is fast but "Resolving deltas" takes a long time, the client CPU or disk is the limit. If the output stalls before any "Receiving objects" line appears, the server is still enumerating or compressing.
"Fewer branches means a smaller clone." Only partly. Branches that share history with the default branch add few objects. Unmerged long-lived branches with their own large files add many. Limiting refs is a cheap first step, but it does not shrink history reachable from the branch you keep.
Summary of the cost model
A full clone costs roughly the sum of five things: ref count, object count (server walk and client indexing), pack size (compression and transfer), delta chain length (client CPU), and checkout size (client disk). Shallow clones reduce the object count by cutting history. Partial clones reduce pack size and transfer by deferring blobs or trees. Sparse checkout reduces checkout size. The next chapters take these one at a time.
Exercise
A repository has 40,000 commits and 300,000 files at HEAD. The command that sums on-disk object sizes by type reports 2.1 GB in blobs and 90 MB in trees. A teammate says "the clone is slow because the working tree has too many files." Using the measurements, what is a better explanation, and which of the three growth axes is the main cost?
Show answer
The 2.1 GB of blobs against 90 MB of trees shows that blob weight dominates, so the cost comes from file contents accumulated across the whole history, not from the number of files in one checkout. The working tree matters only for the final checkout stage. Blob weight is the main axis, which a blob filter in a partial clone targets later in this tutorial.
Exercise
Why does deleting a 500 MB archive in a new commit not make git clone faster for new contributors?
Show answer
The older commits that contained the archive still reference its blob, and a full clone downloads every object reachable from every ref. The delete only changes the newest snapshot. The blob leaves the clone only if history is rewritten so that no commit references it.