Sub topic 2
Why Large Repositories Are Slow to Clone
This chapter assumes you can clone, fetch, commit, and branch, and that you know Git stores commits, trees, and blobs. It explains where the time and disk space go in a full clone. Later chapters use that explanation to choose between shallow and partial clones.
What a full clone transfers
A default git clone copies every object reachable from every branch and tag on the remote. Git has three kinds of object that matter here:
- A commit records a snapshot, its parent commits, an author, and a message.
- A tree is a directory listing that points to blobs and other trees.
- A blob is the content of one version of one file.
Each commit points to one root tree. Most of that tree is shared with the previous commit, so Git stores only the trees and blobs that changed. Even so, the total grows with every change ever made.
A full clone fetches all four blobs for app.py and README, all three trees, and all three commits. In a repository with hundreds of thousands of commits, you download every version of every file that ever existed, including large files that were later deleted.
Where the cost comes from
The cost has several parts, and they do not all scale the same way.
| Cost | What drives it | Where you feel it |
|---|---|---|
| Network transfer | Total size of the packed objects | Slow clone on a poor connection |
| Server work | Counting and compressing objects for the pack | Long "Enumerating objects" and "Compressing objects" phases |
| Disk space | Size of `.git/objects` plus the working tree | Large `.git` directory |
| Local processing | Resolving deltas and indexing the pack | Long "Resolving deltas" phase |
| Checkout | Writing every file of the latest commit | Delay at the end of the clone |
The working tree is only one snapshot. In many large repositories, .git is several times bigger than the checked-out files, because it holds all history.
Measuring it yourself
You can see how much a repository stores by running git count-objects in an existing clone.
git count-objects -vH
git rev-list --count HEAD
git for-each-ref --count=5 --sort=-committerdate refs/count-objects -vH reports object counts and packed size in readable units. rev-list --count HEAD gives the number of commits reachable from the current commit. Compare the pack size with the size of the working tree to see how much of the clone is history.
Which costs can you reduce?
Each later technique targets one part of the table:
- Fewer branches and tags reduce the number of starting points Git must walk.
- A shallow clone cuts the number of commits, and with them the trees and blobs only they reference.
- A partial clone keeps the commit graph but skips some object types until needed.
- Sparse checkout reduces the files written to the working tree and, combined with a partial clone, the blobs downloaded.
Each option gives up something. You lose history, or you gain a dependency on the remote being reachable later. Knowing what a full clone includes tells you what each option removes.
Exercise
A repository has 200,000 commits, and its checked-out files total 300 MB. The .git directory is 6 GB. Which part of the full clone explains the size difference, and which object types make up most of it?
Show answer
The difference is history. The .git directory holds every commit, tree, and every version of every blob ever committed, while the working tree holds only the latest snapshot. Most of the 6 GB is blobs, the stored versions of files, with trees and commits making up a smaller share.