Sub topic 9
Pattern mode for irregular layouts
This chapter assumes you can run git sparse-checkout set in cone mode, and that you know the difference between the repository and the working tree. Cone mode covers most monorepos. This chapter covers the cases where it doesn't.
What pattern mode is
Pattern mode (also called non-cone mode) stores gitignore-style rules in .git/info/sparse-checkout. Git compares every tracked path against those rules to decide whether to write the file to disk.
Cone mode only lets you choose whole directories. Pattern mode can express exclusions, single files, and name-based matches. In exchange, it is slower and easier to get wrong.
You turn it on with --no-cone:
git sparse-checkout set --no-cone '/*' '!/docs/'Quote the patterns. Without quotes, your shell may expand /* or treat ! specially.
This command includes everything at the top level, then excludes docs/. Cone mode can't express that, because it only builds up from "root files plus chosen directories".
How the rules are evaluated
The syntax looks like .gitignore, but the meaning is reversed. A .gitignore rule marks files to hide. A sparse-checkout rule marks files to keep.
- A leading
/anchors the pattern to the repository root. - A trailing
/matches directories only. - A leading
!negates the rule, which removes matching paths from the set. - A pattern with no slash, such as
*.md, matches at any depth. - The last matching rule wins, so order matters.
Because order matters, put broad includes first and narrow exclusions after them. If you write '!/docs/' '/*', the /* rule matches docs/ last and brings it back.
An irregular layout
Suppose you work on services/api, but the build also reads one config file at tools/lint/config.yml. You don't want the rest of tools/.
git sparse-checkout set --no-cone \
'/*.md' \
'/services/api/' \
'/tools/lint/config.yml'
git sparse-checkout listThe checkout now contains the top-level Markdown files, everything under services/api/, and that one file. In cone mode, you would have to take all of tools/lint/.
Another common case is a repository that is mostly wanted, with a few heavy directories to skip:
git sparse-checkout set --no-cone '/*' '!/vendor/' '!/assets/raw/'Changing the set
add works in pattern mode, but it takes patterns instead of directory names. It appends them to the end of the file, so they take priority over earlier rules.
git sparse-checkout add '/tools/lint/'If you need to review or edit many rules, read them from standard input. This replaces the whole list:
printf '%s\n' '/*' '!/docs/' '!/vendor/' | git sparse-checkout set --no-cone --stdingit sparse-checkout reapply re-applies the current rules to the working tree. Use it after editing .git/info/sparse-checkout by hand.
Costs of pattern mode
Pattern mode has three costs, and you should weigh them before choosing it.
| Concern | Cone mode | Pattern mode |
|---|---|---|
| Matching cost | Compares directory names, fast | Checks each path against each rule, slower as rules grow |
| Sparse index | Supported | Not supported |
| Rules you can write | Whole directories only | Files, wildcards, exclusions |
| Risk of mistakes | Low | Order and anchoring errors are easy |
Because the sparse index requires cone mode, pattern mode gives up the faster status and add described in the sparse index chapter. On a very large repository, long rule lists can make every checkout and merge noticeably slower. The Git documentation recommends cone mode for this reason.
Common errors
- Unquoted patterns. The shell expands
/*into root-level paths before Git sees it. - Missing the leading
/.docs/matches any directory nameddocsat any depth, not only the top-level one. - Wrong order. A later broad include can undo an earlier exclusion.
- Forgetting the
--no-coneflag. Recent Git versions default to cone mode forset, so patterns can fail or be treated as directory names.
Exercise
You want everything in the repository except legacy/ and third_party/, using pattern mode. Write the command, and explain why the order of the patterns matters.
Show answer
git sparse-checkout set --no-cone '/*' '!/legacy/' '!/third_party/'. The last matching rule wins, so /* must come first. If it came last, it would match legacy/ and third_party/ again and put them back in the working tree.