Sub topic 1
Why Large Monorepos Get Slow
Prerequisites
This chapter assumes you can do the following:
- Clone a repository, make commits, switch branches, and read
git statusoutput. - Work comfortably in a terminal.
- Describe a monorepo: one repository that holds many projects, services, and libraries.
You do not need any knowledge of Git internals. This chapter introduces the few ideas it needs. It does not teach sparse checkout commands yet. Later chapters cover those. The goal here is to understand which costs grow with repository size, so you can tell which tool fixes which cost.
The problem in one picture
A small repository feels instant. Cloning takes seconds, git status returns before you notice, and switching branches is free. As a monorepo grows to hundreds of thousands of files and years of history, each of those operations slows down. Developers often respond by waiting, by splitting the repository, or by writing wrapper scripts. A better first step is to measure where the time goes.
The cost comes from three separate places, and they respond to different fixes:
- History size. Every version of every file ever committed must be transferred and stored.
- Working tree size. Git writes the files of the current commit to disk, and it must keep track of all of them.
- Per-command scanning. Many commands visit every tracked file to decide whether something changed.
Cost 1: history grows without bound
Git is a distributed version control system. Your clone normally contains the complete history. That means every commit, every directory listing, and every version of every file that has ever existed, including files that were deleted years ago.
Two things make this expensive in a monorepo:
- Many contributors. A repository where hundreds of people commit daily gains commits at a high rate. Even a team of ten engineers produces thousands of commits per year. A company-wide monorepo produces far more.
- Large files and many versions. Each edit to a file stores a new version of that file's content. Git compresses these versions, but a frequently edited file still adds up.
A fresh git clone pays this cost once, up front. A git fetch pays a smaller version of it each time new commits arrive. If you only ever work on one service, most of that downloaded data is content you never open.
You can see the scale for your own repository:
git count-objects -vH
git rev-list --count HEADThe first command reports how much space Git objects use on disk. The second counts the commits reachable from your current commit. Run both in the largest repository you have. The numbers give you a baseline to compare against after you apply the techniques from later chapters.
Cost 2: the working tree grows with the project
The working tree is the set of ordinary files and folders you see on disk and edit. The repository (the .git directory) is where Git keeps the history. These are different things, and the next chapter looks at the difference closely. For now, note one consequence: when you check out a commit, Git writes every tracked file of that commit into the working tree.
In a monorepo with 500,000 files, a checkout writes 500,000 files, even if you only care about 2,000 of them. This has several effects:
- Disk use. The working tree is a full extra copy of the latest content, on top of the compressed copy inside
.git. - Branch switching. Moving between branches rewrites every file that differs between them. In a busy monorepo, two branches that are a few days apart can differ in thousands of files, most of which are in directories you never touch.
- Other tools. Editors, file watchers, search tools, and antivirus software all see those 500,000 files. IDE indexers may take minutes to start, and they may use gigabytes of memory.
Operating systems matter here. Creating many small files is much slower on some file systems than on others, and real-time virus scanning on some desktop systems makes it slower again. The same repository can feel acceptable on one laptop and painful on another.
Cost 3: commands scan everything
Git has to answer a basic question for commands such as git status: which files differ from the last commit? To do this, it keeps an index (also called the staging area). The index is a file in .git that lists every tracked path together with a record of its state, such as size and modification time.
When you run git status, Git compares the index against the working tree. It reads the index in full, then checks file metadata for each entry. The work grows with the number of tracked files, not with the number of files you changed. If you edited one file in a repository of 500,000 tracked files, Git still has to account for the other 499,999.
Git has mitigations. It compares cheap metadata first and only reads file content when the metadata changed. Optional features such as a file system monitor and an untracked cache reduce the scanning further. They help, but the basic cost still scales with the tracked file count.
The following commands pay this scanning cost:
| Command | What it does with the index |
|---|---|
| `git status` | Reads all entries, compares with the working tree and with `HEAD` |
| `git add` | Reads and rewrites the index, may check many paths |
| `git commit` | Writes a tree from the index |
| `git checkout` / `git switch` | Compares trees and updates changed files and index entries |
| `git diff` | Compares the working tree against the index |
You can measure the effect yourself:
time git status
git ls-files | wc -lThe first command reports how long git status takes. The second prints the number of tracked files. Together they show how your own repository compares with the scale described above.
Which cost does each technique address?
Several Git features exist because of these costs. No single one removes all three. This table previews how the rest of the tutorial maps onto the problem:
| Technique | History download | Working tree size | Per-command scanning |
|---|---|---|---|
| Full clone and full checkout | Full | Full | Full |
| Sparse checkout | No change | Reduced | Reduced for working tree checks |
| Partial clone (object filters) | Reduced | No change by itself | No change |
| Sparse index | No change | No change | Reduced further |
| Shallow clone (`--depth`) | Reduced to recent commits | No change by itself | No change |
Read the table by column. If your pain is a slow clone, sparse checkout alone does not help much, because it still downloads everything unless you combine it with a partial clone. If your pain is a slow git status in a huge working tree, a partial clone alone does not help, because all the files are still checked out. Most real monorepo setups combine two or three techniques.
Common misreadings
Some beliefs lead people to pick the wrong fix, so check these early.
- "The repository is slow because it has a lot of code." Code volume matters less than the number of tracked files and the length of history. A repository with 200 huge files can be quicker than one with 400,000 tiny ones.
- "Sparse checkout shrinks my
.gitdirectory." It does not. Sparse checkout controls which files appear in the working tree. The history stays whole unless you also use a partial or shallow clone. - "I should split the monorepo." Splitting trades one problem for others, such as cross-project changes that can no longer be made in a single commit and dependency versions that drift. It is a legitimate choice, but it is a large organizational decision, and Git features often make it unnecessary.
- "A faster disk fixes it." It helps with checkout time. It does not help with history transfer over a network, and it does little for the scanning cost if the number of tracked files is the bottleneck.
A worked diagnosis
Suppose a developer reports that working in the monorepo takes too long. Before choosing any fix, split the complaint into the three costs.
- "Cloning takes 40 minutes." This points to history size and the number of file versions transferred. Look at
git count-objects -vHand the network speed. Candidates: partial clone, shallow clone. - "Switching branches takes two minutes and my editor freezes." This points to working tree size. Candidate: sparse checkout.
- "
git statustakes 20 seconds, even with no changes." This points to scanning. Candidates: sparse checkout to cut the file count, then the sparse index and file system monitor.
A single developer can report all three. In that case the answer is a combination, applied in the order the symptoms hurt most.
The following script collects the numbers needed for this diagnosis in one run. It reads only, and it changes nothing in the repository:
#!/usr/bin/env bash
set -eu
echo "Commits reachable from HEAD:"
git rev-list --count HEAD
echo "Tracked files:"
git ls-files | wc -l
echo "Object storage:"
git count-objects -vH
echo "Time for git status:"
time git status --short > /dev/nullSave it as repo-scale.sh, run it with bash repo-scale.sh inside a repository, and keep the output. You will compare against it in later chapters.
What to remember
- A large monorepo is slow for three separate reasons: history size, working tree size, and per-command scanning.
- Each fix addresses a subset of these reasons. Choose by symptom.
- Measure first. The commands in this chapter give you a baseline.
- The next chapter separates what Git stores in the repository from what it writes to disk. Sparse checkout depends on that distinction.
Exercise
A teammate says "git status takes 25 seconds in our monorepo, so we should switch to a shallow clone with --depth 1." Which of the three costs does the teammate's symptom point to, and will a shallow clone alone fix it? Explain.
Show answer
The symptom points to per-command scanning, because git status compares the index and working tree across all tracked files. A shallow clone reduces history download, but it still checks out every file, so the number of tracked files and the index size stay the same. It will not fix a slow git status. Reducing the working tree with sparse checkout, and later the sparse index, addresses this cost.
Exercise
You need to explain to a manager why enabling sparse checkout did not make a fresh clone download less data. What is the reason, and which additional feature would reduce the download?
Show answer
Sparse checkout only limits which files are written to the working tree. By default the clone still downloads the complete history and all file contents. A partial clone with an object filter, such as --filter=blob:none, avoids downloading file contents that are not needed, and a shallow clone with --depth reduces the commit history.
Exercise
Name the three commands from this chapter you would run to gather a baseline of repository scale, and say what each one measures.
Show answer
git rev-list --count HEAD counts commits reachable from HEAD, which reflects history length. git ls-files | wc -l counts tracked files, which reflects working tree and index size. git count-objects -vH reports object storage on disk. Timing git status with the time command measures the scanning cost directly.