Sub topic 12
Limitations and Common Pitfalls
This chapter assumes you know how to clone, commit, and branch in Git. It also assumes you have used git sparse-checkout set in cone mode and know that a partial clone skips file contents until they are needed. Sparse checkout limits what appears on disk. It does not change what the repository contains, and most pitfalls come from forgetting that difference.
Tools That Need Files Outside the Sparse Set
Git hides excluded files from the working tree. Your build system, linter, or editor does not know that. If services/api imports code from libs/common, and libs/common is not in your sparse set, the build fails with "file not found" errors.
Typical symptoms:
- A build or test run fails on a missing import or header.
- A code generator cannot find a schema file.
- An editor reports unresolved references that exist in the repository.
The fix is to add the missing directory:
git sparse-checkout add libs/commonBefore you start work, find out which directories your target depends on. Many teams keep a list of dependencies for each service, so new contributors know what to include.
Partial Clone Fetches Happen on Demand
With --filter=blob:none, Git downloads file contents only when it needs them. Adding a directory to the sparse set triggers a fetch, which needs network access. Some other commands also trigger fetches:
git log -pon old history fetches the blobs for each diff.git blamefetches the older versions of a file.git checkoutof an old commit fetches the files in your sparse set at that commit.
Offline, these commands fail or stall. On a slow link, a command that looks local may take a long time. To avoid surprises, run git sparse-checkout add while you are online. Do this before a flight, not during it.
Commands That Ignore the Sparse Set
Some commands work on the repository, not on the working tree. They show results for the whole repository, including files you cannot see:
git log,git diff <commit> <commit>, andgit showcan display changes to files that are not on disk.git grepsearches only the working tree by default. It will not find text in excluded files unless you pass a revision, such asgit grep pattern HEAD. With a partial clone, that search can fetch many blobs.git ls-fileslists tracked files that are skipped, with a-vflag marking them. Plainlsshows only what is on disk.
If a search finds nothing, the code may exist but be excluded. Check git sparse-checkout list before concluding it is absent.
Mixing Cone and Pattern Mode
Cone mode takes directory names. Pattern mode takes gitignore-style rules. If you pass patterns to a cone-mode repository, or the reverse, results can be surprising. Check which mode you are in:
git config core.sparseCheckoutCone
git sparse-checkout listPrefer cone mode unless your layout needs exclusions that cone mode cannot express. Cone mode is simpler and works with the sparse index.
Editing the Pattern File by Hand
In cone mode, .git/info/sparse-checkout is generated by Git in a fixed format. Editing it by hand can produce a file that is no longer a valid cone, and Git may fall back to slower behavior or report errors. Use set, add, and disable instead.
The Sparse Index Is Not Universal
The sparse index speeds up commands such as git status and git add, but only when the repository uses cone mode. Older tools that read .git/index directly, such as some IDE integrations and libraries, may not understand it. If a tool misbehaves after you enable it, update the tool or run git sparse-checkout init --cone --no-sparse-index to turn the feature off.
Local Changes and Switching Branches
Switching branches can fail if the target branch changes a file that you have modified in the working tree. This is normal Git behavior. A less obvious case involves files that are in your sparse set on one branch and not on another. Git keeps the sparse set as it is, so a directory that exists only on the new branch may not appear until you add it.
Summary Table
| Pitfall | Cause | Remedy |
|---|---|---|
| Build cannot find a file | Dependency is outside the sparse set | `git sparse-checkout add <dir>` |
| Command is slow or fails offline | Partial clone fetches blobs on demand | Add directories while online |
| Search misses code | `git grep` covers only the working tree | Check the set, or search a revision |
| Odd results from patterns | Cone and pattern mode mixed | Check mode, prefer cone |
| Tool breaks with sparse index | Tool reads the index directly | Update the tool or disable the sparse index |
Exercise
Your teammate's git grep for a function name returns nothing, but the function exists in libs/common. Their sparse set contains only services/api. Give two ways to find the function, and say what each one costs in a partial clone.
Show answer
First, add the directory with git sparse-checkout add libs/common and search again. This fetches the blobs for that directory. Second, search a revision without changing the working tree, for example git grep name HEAD. In a partial clone this may fetch many blobs from the server to search the whole tree. Both need network access.
Exercise
A build fails with "file not found" for a header in libs/common after you cloned with --sparse. What is the likely cause, and which command fixes it?
Show answer
The directory libs/common is outside your sparse set, so its files are not on disk. Run git sparse-checkout add libs/common to check it out.