Sub topic 1
What a clone actually downloads: commits, trees, and blobs
Prerequisites
This chapter assumes you can run git clone, git fetch, git commit, and git branch from the command line. You should also have a rough idea that Git stores commits, trees, and blobs. This chapter makes that idea precise, because every later technique in this tutorial (shallow clones, partial clones, sparse checkout) works by skipping some of these objects.
The three object types
Git stores a project as a database of objects. Three kinds matter here.
- A blob is the contents of one file at one point in time. It has no name and no path.
- A tree is a directory listing. Each entry maps a name to a blob (a file) or to another tree (a subdirectory).
- A commit points to one top-level tree (the project snapshot), to its parent commit or commits, and carries the author, date, and message.
Every object is identified by a hash of its contents. If a file does not change between two commits, both snapshots point to the same blob, so Git stores it once.
Inspecting the objects yourself
You can see these objects in any repository. The following commands print the type of what HEAD refers to, the tree it points to, and the tree's entries.
git cat-file -t HEAD
git cat-file -p HEAD
git cat-file -p HEAD^{tree}The first prints commit. The second prints the commit's tree line, its parent line, and the message. The third lists entries marked blob or tree, each with a hash and a name.
To count what a repository holds, run this:
git count-objects -v
git rev-list --all --objects | wc -lThe second command walks every object reachable from any ref and counts them. On a large repository, that number can reach millions.
What a full clone downloads
A default git clone asks the server for everything reachable from every branch and tag. That means:
- Every commit in the entire history.
- Every tree each of those commits points to.
- Every version of every file, as blobs.
Git then checks out one snapshot, the default branch tip, into your working directory. The working directory is usually a small part of the download. The rest sits in .git, and most of it is old history you may never read.
Where the size comes from
The three object types do not grow at the same rate.
| Object type | What grows it | Typical share of a large repository |
|---|---|---|
| Commits | Number of commits | Small. Each is a few hundred bytes. |
| Trees | Number of commits times directory count | Medium. Each change rewrites the trees on the path to the changed file. |
| Blobs | Every distinct file version | Large. Big or frequently edited files dominate. |
When a file changes, Git writes a new blob. It also writes new trees for each directory above the file, up to the root. A one-line edit deep in a monorepo therefore creates one blob, several trees, and one commit. Git compresses blobs into packfiles using deltas, but the clone still transfers the compressed history of every file.
This table shows the levers the rest of the tutorial uses:
| Technique | Objects skipped |
|---|---|
| Shallow clone | Older commits, and the trees and blobs only they reach |
| Blobless partial clone | Blobs, until needed |
| Treeless partial clone | Trees and blobs, until needed |
| Sparse checkout | Files outside chosen directories in the working tree |
Exercises
Exercise
You change one line in services/api/handler.py. Which new objects does Git create for the resulting commit?
Show answer
One new blob for handler.py, new trees for services/api/, services/, and the root, and one new commit. Unchanged files and sibling directories keep pointing to their existing objects.
Exercise
Which command lists the entries of the root tree of the current commit, and what two entry types can appear?
Show answer
git cat-file -p HEAD^{tree}. Entries are either blob (a file) or tree (a subdirectory).