Sub topic 10
The sparse index for faster status and add
This chapter assumes you can run git sparse-checkout set in cone mode and know that files outside the sparse set are skipped in the working tree. You should also know that Git keeps an index (the staging area) listing every tracked file.
The problem the sparse index solves
Sparse checkout shrinks the working tree, but by default the index still lists every tracked file in the repository. In a monorepo with millions of paths, commands such as git status and git add read and rewrite that whole index. Each file outside your sparse set still gets an index entry marked with the skip-worktree bit.
The sparse index removes that cost. It is an index format that stores one entry for each excluded directory instead of one entry for each file inside it.
How it works
With a sparse index, Git replaces an excluded directory's file entries with a single sparse directory entry. That entry points at the directory's tree object and stands for everything below it. Directories in your sparse set keep their normal file entries.
The index becomes smaller, so Git reads and writes less on every command. The cost of status and add then depends on the size of your sparse set, not the size of the whole repository.
Enabling it
The sparse index requires cone mode. Cone mode matches whole directories, so Git can tell when a complete directory is excluded and collapse it.
git sparse-checkout init --cone --sparse-index
git sparse-checkout set services/apiThe --sparse-index option sets index.sparse to true in your repository config. You can check it:
git config index.sparse
git ls-files --sparseThe --sparse option of git ls-files shows the sparse directory entries. Without it, git ls-files expands them and lists every file path.
To turn the feature off while keeping sparse checkout, run git sparse-checkout reapply --no-sparse-index. Running git sparse-checkout disable restores the full working tree and a full index.
Commands that expand the index
A command that is not sparse-aware needs the full list of files. In that case Git expands the sparse index in memory, which makes the command about as slow as it was without the feature. Most everyday commands are sparse-aware in current Git versions, including status, add, commit, diff, checkout and restore. Some less common commands still expand the index.
To see whether a command expands the index, trace it:
GIT_TRACE2_PERF=1 git status 2>&1 | grep ensure_full_indexIf the output contains ensure_full_index lines, Git expanded the index during that command. No output means it stayed sparse.
When it helps
The sparse index helps most when these conditions hold:
- The repository has a very large number of tracked files.
- You work in a small set of directories.
- You run
status,addandcommitoften.
For a small repository, the gain is too small to notice. Also, git sparse-checkout set can touch paths outside your set if a command needs them. A directory entry that Git has to expand costs time on that command.
Common errors
- Using non-cone mode. The sparse index needs cone mode. Pattern mode with
--no-conecannot collapse directories, so Git ignores the setting or reports an error, depending on version. - Expecting a smaller repository. The sparse index only changes the index file. History and objects in
.gitare unchanged. Partial clone covers object size. - Old Git. Sparse-aware commands improved over several releases. If
statusstill seems slow, checkgit --versionand update first.
Exercise
You enabled cone mode with --sparse-index, but git status is still slow. Name two things you can check.
Show answer
Run the command with GIT_TRACE2_PERF=1 and look for ensure_full_index lines, which show that Git expanded the index. Also check that the repository is really in cone mode (git sparse-checkout list should print plain directory names), that git config index.sparse prints true, and that your Git version is recent enough to have sparse-aware status.
Exercise
What does a sparse directory entry in the index contain, and what does it replace?
Show answer
It contains the tree object ID of an excluded directory, marked with the skip-worktree bit. It replaces the separate entries for every file in that directory.