Sub topic 10
The Sparse Index for Faster Status and Add
This chapter assumes you can already enable cone mode, pick directories with git sparse-checkout set, and explain the difference between the repository and the working tree. It also assumes you know that the index (the staging area) is a file at .git/index. You need Git 2.34 or later. Many commands were added to the sparse index feature in later releases, so a recent version works best.
Why sparse checkout alone does not fix status
Cone mode shrinks the working tree. A checkout of services/api and libs/common writes only those directories to disk. Yet in a very large monorepo, git status and git add can still be slow. The cause is the index.
The index is a sorted list with one entry for every file in the commit you have checked out. Each entry holds the path, the blob ID, the file mode, and cached stat data. With ordinary sparse checkout, files outside your sparse set still get an index entry. Git sets a flag on those entries called skip-worktree, which tells Git not to expect the file on disk. The entry is still there.
For a monorepo with two million files, the index holds two million entries, even if you only have 20,000 files on disk. Git has to do the following work on every command:
- Read the whole index from disk, which can be hundreds of megabytes.
- Parse and allocate memory for every entry.
- Walk the list to compare it with
HEADand with the working tree. - Write the whole index back after any command that changes it, such as
git add.
Skipping files on disk saves stat calls. It does not shrink the index, so the cost of the index grows with the size of the whole repository, not with the size of your sparse set.
What the sparse index changes
The sparse index replaces the many entries for an excluded directory with one entry for the whole directory. This entry is called a sparse directory entry. It records the directory path, with a trailing slash, and the object ID of the tree for that directory. A tree object already describes everything beneath it, so Git loses no information. It can expand the entry back into individual file entries from the tree whenever it needs them.
Git's cost per command is then proportional to the number of entries inside your cone plus the number of collapsed directories. The cost no longer depends on how many files sit in excluded directories.
Git collapses directories at the boundary of the cone. Because cone mode includes all files at the repository root and in each parent directory of a chosen directory, those files stay as normal entries. Only whole directories outside the cone are collapsed. This is why the sparse index requires cone mode. The cone patterns are directory-based, so Git can compute which directories to collapse. Arbitrary gitignore-style patterns from pattern mode can include or exclude individual files, which would make a directory only partly excluded and could not be stored as one tree entry.
Enabling the sparse index
Turn on the sparse index together with cone mode:
git sparse-checkout init --cone --sparse-index
git sparse-checkout set services/apiYou can also pass the option to set:
git sparse-checkout set --cone --sparse-index services/api libs/commonBoth forms change two repository settings: core.sparseCheckout and core.sparseCheckoutCone for cone mode, and index.sparse for the sparse index. You can read them back:
git config --get core.sparseCheckoutCone
git config --get index.sparseBoth should print true. When the option is set, Git rewrites the index the next time it writes it, using the collapsed format. The file gets an extension that marks it as sparse, so older Git versions that do not know the extension refuse to read it instead of misreading it.
Looking at the sparse index
git ls-files is the usual way to list index entries. By default it expands sparse directory entries, so you see the paths of all files, as if the index were full. To see the real contents, add --sparse:
git ls-files --sparseIllustrative output for a cone of services/api:
README.md
docs/
libs/
services/api/main.py
services/api/util.py
services/billing/
tools/The entries ending in / are the sparse directory entries. To see their mode and tree IDs, add --stage:
git ls-files --sparse --stageA sparse directory entry looks like this. The tree ID is shortened here.
040000 3f9c2a1... 0 docs/
100644 a81be07... 0 README.mdMode 040000 marks a tree. Normal file entries show 100644 or 100755. Counting lines shows the effect directly:
git ls-files --sparse | wc -l
git ls-files | wc -lThe first number is the real size of the sparse index. The second number is how many files the commit holds, because that form expands the index in memory.
Which commands benefit
Speed-up comes only from commands that Git has taught to work with sparse directory entries. These include status, add, commit, diff, checkout, reset, restore, merge, stash, and grep, and the list grows with each release. For these commands, Git compares the tree ID in a sparse directory entry with the tree in HEAD. If they match, the whole directory is unchanged, and Git skips it without looking inside.
The gain is largest where the full index is large and the cone is small. For git status, the time changes from reading millions of entries to reading thousands. For git add, the index file is also written back, so writing a smaller file saves time every time you stage.
You do not change how you use these commands. git add services/api/main.py and git status work the same. Only the cost differs.
Index expansion: when the benefit disappears
Some commands still need individual entries for files outside the cone. When this happens, Git expands the sparse index into a full one in memory before continuing. This is called index expansion. It costs about as much as the old full-index behavior, plus the time to build entries from trees. The command still gives correct results, only without the speed-up.
Typical reasons for expansion:
- A command is not yet taught about sparse directory entries in your Git version.
- A pathspec reaches into a collapsed directory, for example
git add docs/orgit grep pattern -- docs/withdocs/outside the cone. - A command asks for a full listing, such as plain
git ls-fileswithout--sparse. - A merge or checkout has a conflict or change inside a collapsed directory, so Git needs the files inside it.
You can check whether a command expanded the index by using Git's trace output:
GIT_TRACE2_PERF=1 git status 2>&1 | grep ensure_full_indexIf the output has a line for ensure_full_index, the command expanded the index. If nothing prints, the command ran on the sparse index. Run this check when a command feels slow, before you suspect something else. The line also shows which command caused it, so you can compare behavior across Git versions.
Staging files outside the cone
Collapsed directories and the sparse set interact when you add files from outside your cone. By default, Git refuses to stage a path outside the sparse set. It prints a hint that names the paths and suggests --sparse. You can override this for one command:
git add --sparse docs/new-page.mdUse this rarely. A file outside the cone is not meant to be edited in this working tree. The more usual fix is to add the directory to the cone:
git sparse-checkout add docsAdding a directory to the cone expands only that directory's sparse entry into file entries. The rest of the index stays sparse.
Turning it off
To keep cone mode but go back to a full index:
git sparse-checkout reset --no-sparse-indexAlternatively, set the configuration directly:
git config index.sparse falseGit expands the index the next time it writes it. If you run git sparse-checkout disable, the working tree becomes complete, so Git also expands the index, and every entry is a normal file entry again. You can leave index.sparse set to true, because with no sparse set there is nothing to collapse.
Compatibility and gotchas
The index is shared by every tool that reads the repository. A sparse index is a format change, so the following checks matter:
- Older Git versions. A Git that does not understand the extension refuses to open the repository, with an error about an unknown required index extension. Check every machine and container image that uses the same clone, for example a build image with an older Git.
- Other Git implementations. Libraries and tools that read
.git/indexdirectly, such as some IDE plugins, code scanners, and older versions of libgit2-based tools, may fail or ignore the collapsed entries. Test the tools you rely on before enabling the sparse index for a whole team. - Scripts that parse
git ls-files. Plaingit ls-filesexpands the index, so scripts get the same output as before. Scripts that use--sparsesee directory entries ending in/, and a loop that treats each line as a file will break on those. - Cone mode only. If you switch to pattern mode with
--no-cone, Git cannot keep a sparse index. It turns the feature off. - Not a substitute for partial clone. The sparse index reduces index size and command time. It does not reduce what you download. For that, combine it with
--filter=blob:none, as described in the partial clone chapters.
Decision criteria
Use the sparse index when your repository is large enough that git status is noticeably slow even with a small cone, you use cone mode, and your Git version and tools support it everywhere the clone is used. Typical thresholds are repositories with hundreds of thousands of files. For a repository with a few thousand files, the full index is small, and the change adds compatibility risk for little gain.
Skip it, or test carefully first, when you share one working directory across many tools, when you depend on an older Git in CI images, or when your daily commands often reach into collapsed directories and trigger expansion.
Exercise
You enabled cone mode with services/api and turned on index.sparse. git ls-files --sparse | wc -l prints 4,200, while git ls-files | wc -l prints 1,850,000. Explain both numbers. Then name one command from this chapter that would make Git expand the index, and say how you would confirm the expansion.
Show answer
The first command shows the real index: file entries inside the cone and at the root, plus one entry for each collapsed directory outside it. That is why it is small. The second command expands the sparse directory entries in memory, so it lists a path for every file in the commit, which is the full count. A command that causes expansion is git add docs/ when docs/ is outside the cone, because the pathspec reaches into a collapsed directory. Plain git ls-files also expands. To confirm, run GIT_TRACE2_PERF=1 git add docs/ 2>&1 | grep ensure_full_index. A matching line means the index was expanded.
Exercise
A teammate enables index.sparse on a shared clone. The next day a build container using an old Git version fails to run git status in that clone, with an error about an unknown index extension. What happened, and give two ways to fix it.
Show answer
The sparse index adds an index extension that is marked as required, so a Git version that does not know it refuses to read the file instead of misreading it. One fix is to upgrade Git in the container image to a version that supports the sparse index (2.34 or later). The other is to turn the feature off for that clone with git sparse-checkout reset --no-sparse-index or git config index.sparse false, and then let Git rewrite the index in the full format.