Sub topic 12
Limitations and common pitfalls
This chapter assumes you have used git sparse-checkout set and git clone --filter=blob:none --sparse. It also assumes you know the difference between cone mode and pattern mode, and what the skip-worktree bit does. Here we look at where the setup breaks down and how to diagnose it.
Sparse checkout limits files on disk, not history
Sparse checkout changes only which files Git writes to your working tree. Commits, trees, and branches are unchanged. This has three consequences.
- It is not an access control mechanism. Anyone who can fetch the repository can fetch every file. Use separate repositories or server-side permissions for secrets.
- Commands that read history, such as
git log,git diff between commits, andgit blame, still see the whole repository. - Commands that read the working tree, such as
git grep,git status, and your build tool, see only the sparse set.
Most pitfalls come from mixing these two views.
Tools that need files outside the sparse set
The most common failure is a build or test that reaches outside the directories you selected.
Typical causes:
services/apiimports code fromlibs/common, but onlyservices/apiwas selected.- A root-level workspace file (
package.json,go.work,pom.xml) lists projects that are missing on disk. - A symlink points to a directory that is not checked out.
- A code generator or lint config lives in a shared
tools/directory. - A submodule path is outside the set.
Cone mode always includes files directly at the repository root, so root configuration files usually exist. The directories they refer to may not.
The error message rarely mentions sparse checkout. You see "module not found", "no such file", or an empty glob. Check the sparse set first.
git sparse-checkout list
git ls-files -t | grep '^S' | headThe first command prints the selected directories. The second lists tracked files marked with the skip-worktree bit, shown with the tag S. If the missing path appears there, Git is deliberately keeping it off disk.
Fix it by adding the dependency directory:
git sparse-checkout add libs/commonIf a project needs a transitive set of directories, keep that list in the repository, for example in a script or a manifest file per service. People then run one command instead of rediscovering dependencies by trial and error.
git sparse-checkout set services/api libs/common tools/codegenThe set command replaces the whole list, while add extends it. Running set with only the new directory is a frequent mistake. It silently drops the earlier directories from your working tree.
Search and status do not show what you cannot see
Because excluded files are not on disk, several everyday commands skip them:
git statusdoes not report excluded files as deleted. Git treats them as intentionally absent.git grepsearches only checked-out paths by default. A search for a function name can return nothing even though callers exist elsewhere. Recent Git versions acceptgit grep --sparseto include paths outside the set. Searching a specific revision, as ingit grep pattern origin/main, reads the commit and ignores the sparse set.- Editors and IDE indexers see only what exists on disk. "Find usages" and refactoring tools miss callers in excluded directories, so a rename can break code you cannot see.
Before a wide refactor, widen the set or search a commit:
git grep -n "LegacyClient" origin/main -- .
git sparse-checkout add services/billingWorking-tree changes and branch switches
Sparse checkout adjusts the working tree when you switch branches or change the set. Several situations can surprise you.
Local files in excluded paths. If you have untracked or modified files in a directory that is being removed from the set, Git leaves them in place and warns that paths are "not up to date and were left despite sparse patterns". The directory stays on disk. Move or commit those files, then run git sparse-checkout reapply.
Merge conflicts outside the set. If a merge or rebase conflicts in a path outside the set, Git writes that file to the working tree so you can resolve it. After you resolve the conflict and commit, run git sparse-checkout reapply to remove the paths that no longer belong.
Empty set. If none of the patterns match anything usable, Git can report an error such as "Sparse checkout leaves no entry on working directory". Fix it by selecting a valid directory or running git sparse-checkout disable.
Config changes in the middle of a rebase. Changing the sparse set while a merge or rebase is in progress makes the state hard to reason about. Finish or abort the operation first.
Partial clone: costs move to later commands
--filter=blob:none makes the clone fast by leaving out file contents. Git then downloads blobs when a command needs them. This is lazy fetching from the promisor remote, which is the server you cloned from.
Problems appear when a command needs many blobs at once.
git log -p,git blame, andgit diffacross old commits each trigger fetches. Git batches some of them, but a long history walk can produce many round trips.- You cannot run these commands offline for any blob you have not already fetched.
- A checkout that expands the sparse set downloads all blobs for the new directories in one batch, which can be large.
- If the server does not support filters, Git ignores the request or fails. Cloning from a host without partial clone support produces a full download or an error.
You can see how much is missing by asking Git which objects are absent:
git rev-list --objects --all --missing=print | grep '^?' | wc -lA large count is normal in a blobless clone. It becomes a problem only when your workflow touches those objects.
Newer Git versions include git backfill, which downloads missing blobs for the paths in your sparse set in large batches. If it is available, it is a better choice than letting git blame fetch objects one at a time.
Shallow clones remove history that CI often needs
The CI recipe from earlier combines --depth 1 with --filter=blob:none --sparse. It is the fastest option, and it fails when a job needs ancestry. These jobs need history:
- "Which files changed against
main" checks. - Version numbers derived from tags or
git describe. - Release notes built from commit ranges.
- Any command that needs a merge base.
With one commit of history, git merge-base HEAD origin/main finds nothing, and diffs against the target branch fail.
git clone --depth 1 --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api
git fetch --deepen=100 origin
git merge-base HEAD origin/maingit fetch --deepen=100 extends the existing shallow history by 100 commits. If the merge base is still missing, deepen again or fetch the target branch directly. Pick a depth that covers the longest-lived pull request in your project, or run git fetch --unshallow for jobs that really need everything. Because the clone is blobless, the extra history costs commits and trees only, which is far cheaper than a full fetch.
Choosing the wrong mode
Cone mode accepts directory names. Pattern mode accepts gitignore-style patterns. Mixing them up gives results that look random.
- In cone mode,
git sparse-checkout set services/apiselects that directory and all of its contents, plus root files and the files directly inside each parent directory. - Cone mode rejects or mishandles wildcard patterns and negations such as
!/docs/. If you need exclusions, you need pattern mode. - In pattern mode,
services/apiis a pattern, not a directory path. It can match in unexpected places, because patterns without a leading slash match at any depth. - Pattern mode is slower on very large trees, because Git checks each path against the patterns. It also cannot use the sparse index. The Git documentation discourages it for most uses.
Check which mode you are in:
git config core.sparseCheckoutCone
cat .git/info/sparse-checkoutIf the file contains lines like /* and !/*/, it is in cone format. If you hand-edit that file in cone mode, Git may reject it or fall back to slower behavior. Use the set, add, and reapply commands instead of editing it.
Sparse index compatibility
The sparse index makes git status and git add faster by collapsing excluded directories into single entries. It works only with cone mode, and not every tool understands it.
- Git commands that have not been adapted expand the index to the full size, which removes the speed benefit. The command still works and is only slower.
- Third-party tools that read
.git/indexdirectly, such as older IDE plugins or libraries based on older Git implementations, may fail on a sparse index or misreport status. - A hook or script that lists files with
git ls-filescan see directory entries instead of file entries if it assumes a full index.
If a tool misbehaves, test with the sparse index off:
git sparse-checkout reapply --no-sparse-indexSwitch it back on later with git sparse-checkout reapply --sparse-index, once you have confirmed that your tools handle it.
Quick reference
| Symptom | Likely cause | First fix |
|---|---|---|
| Build says a module or file is missing | Dependency directory outside the set | `git sparse-checkout add <dir>` |
| `git grep` finds nothing that you know exists | Search covers only checked-out paths | `git grep --sparse` or search a revision |
| Old directory remains after removing it from the set | Local untracked or modified files | Move or commit them, then `git sparse-checkout reapply` |
| `git blame` or `git log -p` is slow | Blobs fetched one at a time | Fetch in batches, or `git backfill` where available |
| `merge-base` fails in CI | Shallow clone too short | `git fetch --deepen=N` |
| Exclusion pattern is rejected | Cone mode cannot express it | Use `--no-cone` deliberately |
| IDE or tool shows wrong status | Tool does not read a sparse index | Turn off the sparse index |
The diagram gives a rough order for diagnosing a missing file.
Summary of habits that avoid trouble
- Treat the sparse set as part of the build configuration. Include every directory a project depends on.
- Prefer cone mode and the commands
set,add,list, andreapplyover hand-edited pattern files. - Use
--depth 1only for jobs that never need history, and deepen explicitly for those that do. - Expect blobless clones to fetch on demand, and avoid history-wide commands on them unless you have batched the download.
- When something looks missing, check the sparse set first, then the commit, then the object download.
Exercise
A CI job clones with --depth 1 --filter=blob:none --sparse, runs git sparse-checkout set services/api, and then runs git diff --name-only origin/main...HEAD to decide which tests to run. The command fails with a message about no merge base. Name the cause and give a fix that keeps the clone fast.
Show answer
The --depth 1 clone contains only one commit, so Git cannot find a common ancestor with origin/main. Deepen the history with git fetch --deepen=100 origin (or a depth that covers your longest-lived branch) before running the diff. Because the clone is blobless, the extra commits and trees are cheap to fetch.
Exercise
You run git sparse-checkout set libs/common on a checkout that previously had services/api selected. Your build for services/api now fails, and git grep no longer finds its code. What happened, and which command should you have used?
Show answer
set replaces the whole sparse set, so services/api was removed from the working tree. To keep both, run git sparse-checkout add libs/common, or git sparse-checkout set services/api libs/common. You can restore the old state with git sparse-checkout add services/api.
Exercise
After resolving a merge conflict in a file outside your sparse set, that file is still on disk. How do you return the working tree to match the sparse set?
Show answer
Commit the resolution, then run git sparse-checkout reapply. Git writes conflicted paths outside the set to the working tree during the merge, and reapply removes the paths that do not belong.