Sub topic 8
How Git treats files outside the sparse set
This chapter assumes you can commit, branch, and merge. It also assumes you have run git sparse-checkout set at least once, as in the earlier chapters. After sparse checkout, some tracked files are not on disk. This chapter explains what Git believes about those files, so you can predict what each command will do.
The index is the source of truth
Git keeps three views of a project:
- Commits store full snapshots, including every file in the repository.
- The index (also called the staging area) is a list of tracked paths, each with the object ID it will have in the next commit.
- The working tree is the set of files on disk.
Sparse checkout changes only the third view. Every path stays in the index and in every commit. Git does not remove outside paths from the index. It marks their index entries with the skip-worktree bit. This is a flag that means "this path's file is intentionally absent from disk, so don't look for it."
Normally, a tracked path whose file is missing counts as a deletion. The skip-worktree bit tells Git not to treat it that way.
Seeing the bit
Run this to build a small repository and inspect it.
git init -b main demo
cd demo
git config user.name "Demo User"
git config user.email "[email protected]"
mkdir -p services/api services/billing docs
echo "readme" > README.md
echo "api code" > services/api/main.txt
echo "billing code" > services/billing/invoice.txt
echo "guide" > docs/guide.txt
git add .
git commit -m "Initial commit"
git sparse-checkout set --cone services/api
ls
git ls-files -tls prints README.md and services. The docs directory is gone, and services contains only api. The -t option of git ls-files prints a status tag before each path:
H README.md
S docs/guide.txt
H services/api/main.txt
S services/billing/invoice.txtH means a normal cached entry. S means the skip-worktree bit is set. All four files are still tracked. Two of them are just not on disk.
What Git does with skip-worktree entries
The rule is simple. For a path with the bit set, Git assumes the working tree matches the index and does not check the disk. Several behaviors follow from that rule.
Status and diff ignore them
git status does not report excluded files as deleted, because Git never looks for them. git diff against the index also skips them. This is the main performance gain on large trees. Git does not call stat on files you did not check out.
Commits keep them
git commit builds the new tree from the index. Excluded entries still sit in the index with their old object IDs, so they pass into the new commit unchanged. Committing from a sparse checkout never deletes the files you cannot see.
History commands still see everything
Commands that read the object database directly do not depend on the working tree. These all work for excluded paths:
git log --oneline -- docs
git show HEAD:docs/guide.txt
git diff HEAD~1 HEAD -- docsgit show HEAD:docs/guide.txt prints the file even though docs/guide.txt does not exist on disk. In a partial clone, this can trigger a download of the missing blob. Chapter 6 covers that.
Switching branches updates the index, not the disk
Suppose the target branch changes docs/guide.txt. Git updates the index entry to the new object ID and leaves the bit set. No file appears on disk. If the target branch adds services/api/extra.txt, that path matches your patterns. Git writes the file and leaves the bit clear.
Git re-evaluates the patterns against every path in the new index on each checkout. A path that newly matches gets written. A path that stops matching gets removed.
Adding outside the set is refused
mkdir -p docs
echo "new page" > docs/new.txt
git add docs/new.txtGit refuses. It reports that the path is outside your sparse-checkout definition and suggests widening the set. Git does this because a file outside the set is probably there by accident, and committing it would silently mix the set's boundaries. If you really mean it, use the override flag:
git add --sparse docs/new.txtUsually the better move is git sparse-checkout add docs, which brings the directory into the set. Then the file is part of your working area.
Conflicts bring files back
Merges and rebases can touch excluded paths. If a path outside the set merges cleanly, Git updates only the index. If it conflicts, Git must show you the conflict markers. It writes the file to disk and clears the bit so you can resolve it. After you resolve and commit, you may see files you did not choose. git sparse-checkout reapply removes them again once they are clean.
Summary of behavior
| Operation | Effect on a path outside the sparse set |
|---|---|
| `git status`, `git diff` | Path is ignored. It is not reported as deleted. |
| `git commit` | Path is carried from the index into the new commit unchanged. |
| `git switch`, `git checkout` | Index entry is updated. No file is written unless the path now matches. |
| `git add <path>` | Refused unless you pass `--sparse`. |
| `git merge`, `git rebase` | Clean paths stay off disk. Conflicted paths are written so you can resolve them. |
| `git log`, `git show <rev>:<path>`, `git diff <rev> <rev>` | Work normally. They read the object database. |
| `git grep` | Searches files on disk by default. Pass a revision to search a whole tree. |
Local changes and leftover files
Sparse checkout never throws away work it cannot recover. Before removing a file, Git checks whether you modified it. Take a modified file in docs/. If you then narrow the set so that docs is excluded, Git does not delete the file. It keeps it and prints a warning that lists paths "not up to date" and left in place despite the sparse patterns. You can commit, stash, or discard the change, then run git sparse-checkout reapply.
Untracked and ignored files also survive. Say services/billing/ holds a build directory that .gitignore covers. When you exclude services/billing, Git removes the tracked files. It does not delete the ignored build output. The directory stays on disk with only those leftovers. This is intended behavior, and it looks like a bug the first time you see it. Delete the leftovers yourself if you want the directory gone.
Two cases to know:
- A new untracked file inside an excluded directory. Git shows it as untracked in
git status. The path may still collide with a tracked, excluded path of the same name. Git will then refuse operations that would overwrite it. - Case-only renames and symlinks. Outside the set, Git only compares index entries. It has no on-disk files to compare, so it cannot warn about collisions that exist only on disk.
Do not use skip-worktree for other purposes
The bit is a general index flag. git update-index --skip-worktree <file> sets it manually. People sometimes use this to "ignore" local changes to a tracked config file. This is fragile.
To see which entries carry the bit, run git ls-files -t and look for S. To clear the bit on one path, run git update-index --no-skip-worktree <path>. Prefer git sparse-checkout commands for anything that sparse checkout manages.
Predicting surprises
The model gives you a short checklist. Before you run a command, ask whether it reads the index and object database, or the disk.
- Commands that read only the index and objects (
log,show,commit, tree-to-treediff) behave identically with or without sparse checkout. - Commands that scan the disk (
grep, recursive build tools, linters, editors' project search) see only the sparse set. This is usually the goal, but it is also the source of "it can't find the file" errors. A build that imports fromlibs/commonwill fail iflibs/commonis excluded. - Commands that write to the disk (
switch,merge,rebase,stash pop) follow the patterns, except when a conflict forces a file onto disk.
Exercise
You run git sparse-checkout set --cone services/api. A teammate's branch changes docs/guide.txt, and you run git switch teammate-branch. Does docs/guide.txt appear in your working tree? What changes in the index?
Show answer
No. The path still does not match your patterns, so its skip-worktree bit stays set and no file is written. Git updates the index entry to the new object ID from the teammate's branch. git show HEAD:docs/guide.txt would now print the teammate's version, because it reads the object database.
Exercise
After narrowing the sparse set, git status prints no deletions, even though docs/ is missing from disk. Why? Then name the command that would show you which tracked paths are currently excluded.
Show answer
Excluded paths have the skip-worktree bit set in the index, so Git assumes the working tree matches the index and does not check the disk for them. Running git ls-files -t lists tracked paths with a tag, and the paths tagged S are the excluded ones.
Exercise
You edit docs/guide.txt, then run git sparse-checkout set --cone services/api, which excludes docs. What does Git do with your edit, and how do you finish the narrowing?
Show answer
Git keeps the modified file on disk and warns that it was left despite the sparse patterns, because removing it would lose your uncommitted work. Commit, stash, or discard the change, then run git sparse-checkout reapply so Git can remove the file.