Sub topic 9
Pattern mode for irregular layouts
This chapter assumes you have read the earlier chapters of this tutorial. You should be comfortable with git sparse-checkout set in cone mode, with the difference between the repository and the working tree, and with partial clones that use --filter=blob:none. You should also know how to read a .gitignore file.
Cone mode is the right default. Some repositories do not fit its model, though. Pattern mode, also called non-cone mode, lets you describe the working tree with gitignore-style rules. It is more flexible and also slower, harder to get right, and incompatible with the sparse index.
What cone mode cannot express
Cone mode accepts a list of directories. For each directory you name, Git includes everything inside it, plus the files directly inside each of its parent directories. It cannot do any of the following:
- Include everything except one or two directories.
- Select directories with a wildcard, such as the
include/folder of every library underlibs/. - Select files by name or extension at any depth, such as every
*.protofile. - Include a directory but leave out one of its subdirectories.
Suppose a monorepo looks like this:
README.md
Makefile
docs/
vendor/
libs/auth/include/
libs/auth/src/
libs/billing/include/
libs/billing/src/
services/api/
services/api/fixtures/
services/web/
schemas/orders/order.proto
schemas/users/user.protoYou want the root files, services/api without its large fixtures/ directory, the public headers in every libs/*/include/, and the .proto files wherever they are. No list of cone directories describes that. A list of rules does.
Turning pattern mode on
Since Git 2.37, git sparse-checkout set uses cone mode unless you say otherwise. To use patterns, pass --no-cone. The arguments are then patterns, not directory names.
git sparse-checkout set --no-cone '/*' '!/docs/'Quote every pattern. An unquoted * is expanded by your shell, and in an interactive Bash session an unquoted ! can trigger history expansion.
Git writes the patterns to .git/info/sparse-checkout and sets two settings:
git config core.sparseCheckout
git config core.sparseCheckoutConeThe first prints true. The second prints false in pattern mode. Git reads the file differently depending on that second setting. In cone mode it parses the file as a rigid set of directory rules. In pattern mode it treats each line as a general pattern.
> Recent Git documentation describes non-cone mode as deprecated and recommends cone mode wherever it works. Pattern mode is still supported. Use it when you have a layout that cone mode cannot describe, and keep the pattern list small.
How the patterns are read
The syntax follows .gitignore, with one important difference in meaning. In a .gitignore, a match means "ignore this". In the sparse-checkout file, a match means "include this path in the working tree". A ! in front of a pattern reverses it, so the path is excluded.
| Pattern | Meaning |
|---|---|
| `/*` | Every entry at the repository root: the root files and every top-level directory. |
| `!/docs/` | Exclude the top-level `docs` directory. The leading slash anchors it to the root. |
| `/services/api/` | Include the directory `services/api` and everything under it. The trailing slash matches directories only. |
| `/libs/*/include/` | Include the `include` directory of each direct child of `libs`. `*` does not cross a slash. |
| `*.proto` | Include files ending in `.proto` at any depth. A pattern with no slash matches at any level. |
| `**/fixtures/` | Match any `fixtures` directory at any depth. |
Three rules decide most outcomes:
- Anchoring. A pattern with a slash at the start or in the middle is anchored to the repository root.
services/api/and/services/api/mean the same thing. A pattern with no slash, or only a trailing one, such asfixtures/, can match at any depth. - Last match wins. Git reads the list from top to bottom, and the last pattern that matches a path decides. A
!rule only takes effect if it comes after the rule it is meant to override. - Files first, then parents. Git tests the file path itself. If no pattern decides, it tests the parent directory, then that directory's parent, up to the root. This is how
/services/api/includesservices/api/src/main.py: the file matches nothing, but its parent directory does.
The third rule explains a common surprise. A pattern with a trailing slash only matches directories. When you write /docs/, the files inside docs/ are decided by their parent directory, not matched directly.
A worked example
Here is the set of rules for the layout above:
git sparse-checkout set --no-cone \
'/*' \
'!/*/' \
'/services/api/' \
'!/services/api/fixtures/' \
'/libs/*/include/' \
'*.proto'Read it from top to bottom:
/*includes everything at the root.!/*/then excludes every top-level directory. Only root files such asREADME.mdandMakefilesurvive, because a file is not a directory and the trailing slash stops the rule from matching it./services/api/bringsservices/apiback.!/services/api/fixtures/carves the large subdirectory back out. Cone mode cannot do this./libs/*/include/includes the public headers of each library.*.protoincludes every.protofile, even in directories that rule 2 excluded, because Git tests the file itself before it looks at its parents.
Check the result before you rely on it:
git sparse-checkout list
git ls-files -t | grep '^S' | headgit sparse-checkout list prints the stored patterns. In git ls-files -t output, a leading S marks an entry that is in the index but marked skip-worktree. Those are the paths Git left out of the working tree.
Some Git versions also provide git sparse-checkout check-rules. It reads paths from standard input and reports which ones the rules would include, so you can test a rule set against sample paths without changing your working tree. Run git sparse-checkout -h to see whether your version has it.
Editing the pattern list
The management commands still work, with different effects.
git sparse-checkout add '/tools/' '*.sql'
git sparse-checkout set --no-cone '/*' '!/vendor/'
git sparse-checkout reapply
git sparse-checkout disableaddappends patterns to the end of the file. Because the last match wins, an appended pattern can override an earlier one.set --no-conereplaces the whole list. It is the safest way to make a large change, because you see the full result in one command.reapplyrereads the file and updates the working tree. Use it after you edit.git/info/sparse-checkoutby hand.disableturns sparse checkout off and restores the full working tree.
If you edit the file by hand, stay in pattern mode. If core.sparseCheckoutCone is true, Git expects the strict cone format and may rewrite or reject lines that do not fit it.
You can also load rules from a file you keep in version control, so the team shares one definition:
git sparse-checkout set --no-cone --stdin < tools/sparse-rules.txtEach line of tools/sparse-rules.txt is one pattern, written without shell quotes. The file itself must be inside the sparse set, or you must keep a copy outside the repository.
Costs of pattern mode
Pattern mode costs more than cone mode in three ways.
Matching is slower. In cone mode, Git stores directories in a hash table and can decide a path with a few lookups. In pattern mode it may test a path against every pattern in the list, and it does this for each path in the index. On a repository with hundreds of thousands of files and a long pattern list, commands like git status and git checkout spend visible time on matching. Keep the list short, and prefer anchored patterns such as /libs/*/include/ over broad ones like **/include/.
The sparse index is unavailable. The sparse index collapses excluded directories into single entries. That only works when Git can tell from the rules that an entire directory is excluded, and only cone mode guarantees this. With patterns, Git keeps a full index, so you do not get the faster status and add described in the sparse index chapter.
Mistakes are silent. Git does not check patterns against your tree. A typo, a missing slash, or a rule in the wrong order produces a working tree that looks plausible but lacks files. A common case is !/docs without the trailing slash. It matches a file named docs but does not exclude the directory the way you meant.
> Rules that exclude a directory do not delete it from the repository. Git still tracks every excluded file, and a later git sparse-checkout disable or a wider rule list brings them back. The skip-worktree bit only controls whether the file appears on disk.
Pattern mode with a partial clone
Pattern mode combines with a partial clone the same way cone mode does. The initial --sparse clone starts in cone mode with only the root files, so switch to patterns afterward:
git clone --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set --no-cone '/*' '!/docs/' '!/vendor/'Git fetches the file contents that your rules select in one batch when it updates the working tree. A rule that selects many scattered files, such as *.proto across a large tree, can still trigger many fetches. Narrow rules fetch less.
Choosing between the modes
| Situation | Better choice |
|---|---|
| You work in a few whole directories | Cone mode |
| You need the sparse index for speed | Cone mode |
| You want everything except a few directories | Pattern mode |
| You need wildcards across sibling directories | Pattern mode |
| You select files by extension or name at any depth | Pattern mode |
| Many developers share the setup | Cone mode, because it is easier to review and harder to get wrong |
If you find yourself writing more than a handful of patterns, consider reorganizing the tree or listing more directories in cone mode, and accept a few extra files on disk.
Exercise
A monorepo has top-level directories apps/, libs/, docs/, and third_party/, plus root files. You want everything except docs/ and third_party/. Write the git sparse-checkout command in pattern mode, and explain why cone mode cannot do this.
Show answer
Run git sparse-checkout set --no-cone '/*' '!/docs/' '!/third_party/'. The first pattern includes every root entry, and the two ! patterns come after it, so they win for those directories. Cone mode only accepts a list of directories to include, so you would have to name apps and libs and every other directory you want, and it has no way to say "everything except".
Exercise
Given these rules in order, which of these paths are included: README.md, libs/auth/include/auth.h, libs/auth/src/auth.c, schemas/users/user.proto?
/
!//
/libs//include/
.proto
Show answer
README.md is included, because /* matches it and no later rule excludes a file. libs/auth/include/auth.h is included, because its parent directory libs/auth/include matches /libs/*/include/. schemas/users/user.proto is included, because the file itself matches *.proto, and Git tests the file before its parents. libs/auth/src/auth.c is excluded: the file matches nothing, libs/auth/src matches nothing, libs/auth matches nothing, and libs matches !/*/.