Sub topic 2
What Git Stores Versus What You See on Disk
This chapter assumes you can clone, commit, branch, and read git status. It also assumes you know the command line and what a monorepo is. Sparse checkout only makes sense once you separate two things Git keeps apart: the repository data and the working tree. This chapter builds that model.
The three places your files live
A Git clone holds your project in three places at once.
- The object database in
.git/objects. It holds every commit, tree, and blob (file content) that Git has downloaded. It is the history. - The index, a single file at
.git/index. It is the staging area. It lists every path in the commit you are on, along with the object ID of its content and cached file metadata. - The working tree, the ordinary files and directories you open in your editor.
Sparse checkout changes only the third place, and the index records which paths are left out. The object database is not affected by it. Partial clone, covered in a later chapter, is what changes the first place.
What the object database contains
Git stores four kinds of objects. Three matter here.
- A blob is the content of one file, with no name attached.
- A tree is a directory listing. Each entry gives a name, a mode, and the ID of a blob or another tree.
- A commit points to one root tree and to its parent commits.
A commit therefore describes a complete snapshot of the project. To rebuild the snapshot, Git starts at the commit, reads the root tree, and follows the trees down to the blobs.
You can inspect this directly. Run these in any repository:
git cat-file -p HEAD
git cat-file -p HEAD^{tree}The first command prints the commit, including a tree line. The second prints the root directory listing: entries of type blob for files and tree for subdirectories.
Identical content is stored once. If a file is the same in 500 commits, there is one blob and 500 trees that point to it. This is why history is cheaper than it looks, and why the cost of a large monorepo comes mostly from the number of distinct paths and versions, not from the number of commits alone.
What the index contains
The index has one entry per file in the commit you have checked out. Each entry records:
- the path,
- the mode (regular file, executable, symlink),
- the object ID of the staged content,
- cached file system data, such as modification time and size, that lets Git skip re-reading files that have not changed.
The cached data is what makes git status fast in a small repository. Git compares the cached values to the file system and only hashes files that look different. In a monorepo with a million paths, that comparison still has to visit every entry, which is one of the reasons commands slow down as the tree grows.
You can see the index contents with:
git ls-files --stage | headEach line shows a mode, an object ID, a stage number (normally 0), and a path.
What the working tree is
The working tree is the set of files you see and edit. Git builds it from the index when you check out a branch. Git writes a file to the working tree by reading the blob the index points to and creating a file on disk.
Three consequences follow.
- The working tree is a copy. Deleting it does not delete history.
git restore .can write it back. - Its size scales with the number of paths checked out, so cost grows with the tree and not only with your change.
- Git has to examine it.
git statuscompares the working tree to the index, andgit checkoutorgit switchrewrites files that differ between two commits.
The working tree is the part you can shrink without losing any history.
Why monorepo slowness has two sources
Because of this split, a large repository can be slow in two separate ways. Knowing which one you have decides which tool you reach for.
| Slow operation | Cause | Part of Git involved | Tool that helps |
|---|---|---|---|
| Clone or fetch takes a long time and uses much disk | Many objects must be downloaded and stored | Object database | Partial clone, shallow clone |
| Checkout and switch take a long time | Many files must be written or rewritten | Working tree | Sparse checkout |
| `git status` and `git add` are slow | Many index entries and files must be compared | Index and working tree | Sparse checkout, sparse index |
| Editors and file watchers use heavy CPU | Many files exist on disk | Working tree | Sparse checkout |
Sparse checkout addresses the second, third, and fourth rows. It does not address the first one by itself. If you run git sparse-checkout set after a normal full clone, you have already downloaded every blob. You save working tree space and time, but not the download or the size of .git.
Seeing the difference in practice
The commands below use a full clone, so you can see the object database and working tree sizes separately. Use any repository you already have. A repository with several top-level directories shows the effect best.
git clone https://example.com/org/monorepo.git
cd monorepo
git count-objects -vH
git ls-files | wc -l
find . -path ./.git -prune -o -type f -print | wc -lgit count-objects -vH reports the size of the object store. git ls-files | wc -l counts the paths in the index. The find command counts files on disk outside .git. After a normal clone, the last two numbers match, because every indexed path has a file.
Now restrict the working tree:
git sparse-checkout set services/api
git ls-files | wc -l
find . -path ./.git -prune -o -type f -print | wc -l
git count-objects -vHThe index still lists every path, so the first count is unchanged. The count of files on disk drops sharply. The object store size is unchanged, because Git did not delete any blobs.
This shows the key fact of the chapter: the index still describes the whole commit, and the working tree shows only part of it.
What does not change when you narrow the working tree
When the working tree shrinks, several things stay the same. This matters because it is easy to assume otherwise.
- History is complete.
git log,git blameon a checked-out file, andgit diffbetween commits work as before. Commands that read only commits and trees do not need files on disk. - Branches and tags are unaffected. You can switch branches. Git updates only the files in the sparse set.
- Commits are whole-snapshot. When you commit, the new commit's tree still includes every path. Paths outside the sparse set carry over unchanged from the parent commit, because the index still holds their entries.
- Other people's work is not hidden from Git. If someone changes a file outside your set and you pull, Git updates the index entry and the object database. It does not write the file to disk.
A commit made with a narrowed working tree does not delete the unchecked-out directories. This is the most common worry, and it is unfounded. A path leaves a commit only if its index entry is removed, and sparse checkout does not remove index entries.
The two ways to reduce what you hold
Sparse checkout and partial clone are separate features that work at different layers. They are often used together.
| Feature | Layer it reduces | What is still present |
|---|---|---|
| Sparse checkout | Working tree (files on disk) | All objects already downloaded, and all index entries |
| Partial clone | Object database (blobs downloaded) | All commits and trees, and the full index |
| Shallow clone (`--depth`) | Commit history | Full trees for the commits that were fetched |
A shallow clone cuts the number of commits, so older history is missing. A partial clone keeps the commits but delays fetching blobs. Sparse checkout leaves both alone and limits only what is written out. Because they act on different layers, you can combine them. A later chapter shows that combination.
A mental model for the rest of the tutorial
Think of the repository as a library catalog and the working tree as the books on your desk.
- The catalog (commits, trees, and the index) lists every book.
- The shelves (the object database) hold the books, or in a partial clone, hold some of them and can fetch the rest on request.
- Your desk (the working tree) holds only the books you are using.
Putting a book back on the shelf does not remove it from the catalog. Taking fewer books to your desk does not make the library smaller. Each tool in this tutorial changes one of these layers, and most surprises come from forgetting which one.
Common misunderstandings
- "Sparse checkout deletes the other directories from the repository." It does not. It removes files from the working tree only. The objects and the index entries remain.
- "After sparse checkout,
.gitis small." Only if you cloned with a filter. A full clone followed by sparse checkout keeps a full-size.git. - "I can commit without the other directories, so I will lose them." Commits copy the index, which still lists everything.
- "
git ls-filesshows what is on disk." It shows what is in the index. Usegit ls-files -tor the file system to see what is checked out. - "Files outside the set are gone, so tools cannot find them." That part is true for tools that read the disk, such as compilers and linters. Git can still see the files, but your build cannot. The pitfalls chapter returns to this.
Exercise
You run a normal git clone of a monorepo, then git sparse-checkout set services/api. Afterwards you compare git ls-files | wc -l with a count of files on disk outside .git. Which count is larger, and why? Also say whether .git got smaller.
Show answer
The git ls-files count is larger. It lists every path in the index, and sparse checkout does not remove index entries. It only marks paths outside the sparse set so Git does not write them to the working tree, so the on-disk count is much smaller. .git did not get smaller, because the clone was a full clone and every blob was already downloaded. Sparse checkout does not delete objects. To avoid the download, you would have to clone with a filter such as --filter=blob:none.
Exercise
A teammate narrows their working tree to libs/common and commits a one-line change there. They worry the commit will delete services/ and docs/ from the repository. Is the worry justified? Explain using the index.
Show answer
No. A commit's tree is built from the index, and the index still has an entry for every path in the previous commit, including those in services/ and docs/. Sparse checkout only affects whether those files are written to disk. The new commit therefore contains the changed file in libs/common and carries over every other path unchanged.
Exercise
Match each problem to the layer that fixes it: (a) the clone downloads too many file contents, (b) git switch rewrites too many files on disk, (c) the history has too many old commits.
Show answer
(a) Partial clone, which acts on the object database by deferring blob downloads. (b) Sparse checkout, which acts on the working tree by limiting which files are written. (c) Shallow clone with --depth, which limits the number of commits fetched.