Sub topic 1
Why Large Monorepos Get Slow
Prerequisites
This chapter assumes you can clone a repository, commit, branch, and read git status output. You should be comfortable at the command line and know roughly what a monorepo is: one repository that holds many projects, services, and libraries. No sparse checkout knowledge is needed. This chapter explains the problem that sparse checkout solves, and later chapters cover the commands.
What "large" means for Git
A monorepo grows along three axes, and each one slows a different Git operation.
- History size. Every commit, tree, and file version ever recorded is stored. Years of work by many teams add up to many gigabytes.
- File count. A checkout may contain hundreds of thousands of files, even if each is small.
- Commit rate. Many teams pushing to one branch means your local copy falls behind quickly, so fetches carry more data.
Git handles large repositories better than many people expect. The slowdowns appear when your machine does work proportional to the whole repository, even though you only touch a small part of it.
Where the time goes
Four common operations show the pattern.
| Operation | Why it slows down |
|---|---|
| `git clone` | Downloads all history and, by default, every file version in it. |
| `git checkout` / `git switch` | Writes every tracked file that differs between two commits into the working tree. |
| `git status` | Compares the index and working tree, which means checking file metadata for every tracked file. |
| `git add` and `git commit` | Read and rewrite the index, which has one entry for every tracked file. |
The index is the file at .git/index that records each tracked path and its last known state. Its size grows with the number of tracked files. Git reads it on almost every command, so a large index taxes even simple commands.
Disk and tooling add to the cost. Editors index every file they see. File watchers and antivirus scanners react to each change. Build tools may scan directories you never build. A developer working on one service pays for the other 200.
The key observation
Most developers need a small slice of a monorepo. A person on the billing service rarely opens the mobile app, the docs site, or another team's libraries. Yet a default clone gives them everything.
Git can separate two things:
- The repository data in
.git: commits, trees, and file contents. - The working tree: the files you see and edit on disk.
Sparse checkout reduces the second. Partial clone, covered in a later chapter, reduces the first. The next chapter explains this split in more detail. The diagram shows where each cost comes from.
Measuring your own repository
Before changing anything, check what you are dealing with. These commands show the size of the problem.
git count-objects -vH
git ls-files | wc -l
time git statusgit count-objects -vH reports how much space the repository data uses. git ls-files | wc -l counts tracked files, which is also the number of index entries. time git status gives a baseline you can compare against after you apply the techniques in later chapters.
Common mistakes in diagnosis
- Blaming the network for everything. A clone is slow because of data volume, but a slow
git statushas nothing to do with the network. It comes from the file count. - Assuming a shallow clone fixes it.
--depth 1trims history but still checks out every file, so a large working tree stays large. - Deleting files locally. Removing directories by hand makes Git report them as deleted changes. Sparse checkout exists to exclude files in a way Git understands.
Exercise
A developer clones a monorepo with git clone --depth 1. The clone is fast, but git status is still slow. Why?
Show answer
The shallow clone only limits history. Git still checks out every tracked file and the index still has one entry per file, so git status must examine all of them. Reducing the working tree, for example with sparse checkout, addresses this.
Exercise
Name the two places where a full clone of a monorepo stores or writes data, and say which one holds the files you edit.
Show answer
The repository data in .git (commits, trees, file contents) and the working tree. The working tree holds the files you edit.