Sub topic 8
How Git Treats Files Outside the Sparse Set
This chapter assumes you can clone, commit, and branch, and that you have run git sparse-checkout set at least once. It explains what Git does with files that sparse checkout leaves out, so the behavior of status, checkout, and merge stops being surprising.
Excluded files stay in the index
Sparse checkout changes the working tree. It does not change history or the index. Every tracked file still has an index entry, and the next commit still contains every file. Git simply does not write the excluded files to disk.
To remember which entries are intentionally absent, Git sets a flag on each one called the skip-worktree bit. The bit tells Git: "This file is missing from disk on purpose. Do not report it as deleted, and do not look for changes in it."
You can see the bit with git ls-files -t. Entries marked S have skip-worktree set. Entries marked H are normal.
git sparse-checkout set --cone services/api
git ls-files -t | headA typical result looks like this:
S README-legacy.md
H README.md
S services/billing/main.go
H services/api/main.goSkip-worktree and assume-unchanged
Both flags tell Git to skip a file during change detection, but they exist for different reasons.
| Flag | Purpose | Set by |
|---|---|---|
| skip-worktree | The file is intentionally absent or ignored locally | Sparse checkout, or `git update-index --skip-worktree` |
| assume-unchanged | A performance hint that the file has not changed | `git update-index --assume-unchanged` |
Sparse checkout uses skip-worktree. Git keeps it in sync with your sparse patterns, so you should not set it by hand on sparse-managed paths.
What happens during common operations
The diagram below follows a single file outside the sparse set through the usual operations.
Status and commit
git status does not list excluded files as deleted. git commit carries them forward from the index unchanged, so committing from a sparse working tree never removes files you cannot see.
Switching branches
When you run git switch to another branch, Git updates the index for every path. For paths inside the sparse set it also updates the files on disk. For paths outside the set it updates only the index entry and keeps the skip-worktree bit. Nothing is written, so switching can be much faster than in a full checkout.
Merges and conflicts
If a merge touches a file outside the sparse set, Git handles it in the index when it can. When a file has a real content conflict, Git must write the conflicted file to disk so you can resolve it. It then clears the skip-worktree bit for that path. After you resolve and commit, the file can remain on disk as an extra file until you reapply the sparse patterns with git sparse-checkout reapply.
Files you create outside the set
If you create a file in an excluded directory, Git does not treat it as a normal tracked file. Depending on the command, Git may leave it alone, warn you, or refuse to add it. Keep your work inside the sparse set, or add the directory first.
git sparse-checkout add services/billingReapplying the patterns
If the working tree drifts from the patterns, for example after a conflict or a manual edit, restore it with:
git sparse-checkout reapplyThis recomputes which entries should be present, sets or clears the skip-worktree bits to match, and then adds or removes files on disk. Files with uncommitted changes that would be removed are kept, and Git warns about them.
Common errors
- A file seems to be missing. Check
git ls-files -t path. If it showsS, the path is outside the sparse set. Rungit sparse-checkout addfor its directory. - Unexpected files remain on disk after a merge. Run
git sparse-checkout reapply. - A script fails because it reads a file that is not on disk. The file is tracked, but excluded. Add its directory to the set.
Exercise
You run git sparse-checkout set --cone services/api, then commit a change to services/api/main.go. Does the commit delete services/billing/main.go from the repository because it is not on disk? Explain what Git uses to decide.
Show answer
No. The commit is built from the index, and the index still holds an entry for services/billing/main.go, marked with the skip-worktree bit. The bit tells Git the file is absent on purpose, so status does not report it as deleted and commit carries the entry forward unchanged.
Exercise
After resolving a merge conflict in a file outside your sparse set, the file is still on disk. Which command brings the working tree back in line with your sparse patterns?
Show answer
git sparse-checkout reapply. It recomputes the skip-worktree bits from the patterns and removes the extra file from disk, unless the file has uncommitted changes.