Sub topic 4
Cone mode: choosing directories
This chapter assumes you can clone, commit, and read git status, and that you are comfortable in a shell. You also need the idea from the previous chapters that Git keeps history in the repository and writes only a selected snapshot into the working tree. Here you will learn how cone mode picks which directories appear on disk, and why Git recommends it over the alternative.
What cone mode is
Cone mode is a restricted form of sparse checkout. You name directories, and Git works out the rest. You never write patterns. The name comes from the shape of the result: the selected directories and their ancestors form a cone-shaped slice of the tree.
You turn it on with:
git sparse-checkout set --cone services/apiThis command does three things:
- It sets
core.sparseCheckouttotrueandcore.sparseCheckoutConetotruein the repository configuration. - It writes the cone patterns to
.git/info/sparse-checkout. - It updates the working tree, removing files that are now excluded and keeping or restoring files that are included.
In Git 2.37 and later, cone mode is the default for git sparse-checkout set and init, so --cone is optional there. Passing it explicitly is harmless and makes scripts work on older versions too.
The three inclusion rules
Cone mode follows three rules. Learn them and you can predict the working tree without running anything.
- Every file directly in the repository root is included.
- For each ancestor directory of a selected directory, the files directly inside that ancestor are included. Its other subdirectories are not.
- Everything under a selected directory is included, recursively.
Suppose you select services/api. Rule 1 keeps README.md at the root. Rule 2 keeps the files directly in services/, but not services/billing/. Rule 3 keeps all of services/api/, including deeply nested files. A top-level directory such as docs/ is left out. It is not an ancestor of a selected directory, and it is not selected.
The file .git/info/sparse-checkout then looks like this:
/*
!/*/
/services/
!/services/*/
/services/api/Read it from the top. /* includes everything at the root. !/*/ then excludes every directory at the root. /services/ re-includes the services directory, and !/services/*/ excludes its subdirectories again. /services/api/ finally includes the selected directory with all its contents. The pattern repeats one level deeper for each level of ancestry.
Try it on a throwaway repository
The script below builds a small monorepo and applies cone mode. You can run it as shown.
set -e
git init demo
cd demo
mkdir -p services/api services/billing libs/common libs/other docs
echo "root" > README.md
echo "services" > services/README.md
echo "api" > services/api/app.py
echo "billing" > services/billing/bill.py
echo "libs" > libs/README.md
echo "common" > libs/common/util.py
echo "other" > libs/other/x.py
echo "guide" > docs/guide.md
git add .
git -c user.name=demo -c [email protected] commit -m "initial"
git sparse-checkout set --cone services/api libs/common
find . -path ./.git -prune -o -type f -print | LC_ALL=C sortThe final command prints:
./README.md
./libs/README.md
./libs/common/util.py
./services/README.md
./services/api/app.pyEach rule shows up in the output:
README.mdis a root file (rule 1).libs/README.mdandservices/README.mdare files directly in ancestors of the selected directories (rule 2).libs/common/util.pyandservices/api/app.pyare inside selected directories (rule 3).services/billing,libs/other, anddocsare absent from disk. They are still in the repository, andgit ls-tree -r HEADlists them.
Selecting several directories
set takes any number of directories and replaces the whole previous selection with them. The call above selected two directories at once. The cone is the union of all the selected directories and their ancestors.
Some details about the arguments:
- Paths are directories relative to the repository root, such as
services/api. A trailing slash is accepted. - Selecting a directory and one of its descendants is redundant.
set services libs/common services/apiis the same asset services libs/common, becauseservicesalready covers everything below it. - Selecting a directory that does not exist in the current commit is allowed. The cone patterns are written anyway, and the directory appears if a later checkout contains it. A mistyped path therefore produces no error. It only produces a working tree that lacks the files you expected.
What the cone gives you that patterns do not
Cone mode is restricted on purpose, and the restriction buys three things.
Speed. With arbitrary patterns, Git may have to test each path against every pattern, in order, because later patterns can override earlier ones. With cone patterns, Git builds a hash set of the selected directories and of their ancestors. Checking a path then costs about one lookup per path component, whatever the number of selected directories. On a repository with millions of paths, this difference is large.
Predictability. Because you choose directories, the result matches how people reason about a monorepo: "I own services/api, and I depend on libs/common." There are no negated patterns whose order matters.
Sparse index support. The sparse index, covered in a later chapter, works only with cone mode, because it needs the boundary of the selection to be a set of whole directories.
The cost is expressiveness. You cannot select one file inside a directory without selecting the whole directory, and you cannot say "everything except docs/". Layouts that need either one need pattern mode, which a later chapter covers. Git's own documentation now discourages non-cone mode for this reason.
Working from a subdirectory
Paths you give to set are interpreted from the repository root, regardless of your current directory in recent Git versions. After sparse checkout removes your current directory from disk, your shell may be left in a directory that no longer exists. If you see "No such file or directory" errors after a set, run cd to a path that is still present, for example the repository root.
Inspecting and verifying the selection
In cone mode, git sparse-checkout list prints the selected directories, not the raw patterns:
git sparse-checkout listFor the demo above it prints libs/common and services/api. To see the underlying patterns, read the file:
cat .git/info/sparse-checkoutCheck which mode is active with:
git config core.sparseCheckout
git config core.sparseCheckoutConeBoth should print true. If core.sparseCheckoutCone is false or unset, Git treats the file as arbitrary patterns, and list prints those patterns instead of directories.
Edge cases
Files in an ancestor directory are always present. You cannot exclude services/README.md while selecting services/api. If a directory such as services/ holds large files directly, they come along with every cone that passes through it. When you design a monorepo for sparse use, put big or noisy files in leaf directories, not in shared ancestors.
Root files are always present. The same applies to files at the repository root. Keep the root small: build manifests and configuration, not data.
Names with special characters. Cone mode escapes glob characters such as *, ?, and [ in directory names. You can pass the plain directory name and Git stores it correctly.
Local modifications to excluded paths. If a tracked file inside a directory you are about to exclude has uncommitted changes, set will refuse to remove it and leave the file in place with a warning. Commit, stash, or discard the changes, then run git sparse-checkout reapply to apply the selection again.
Switching branches. The selection is stored per working tree and applies to whichever commit you check out. A directory that exists on one branch and not on another is simply present or absent, according to your cone.
Reapplying. git sparse-checkout reapply re-reads the stored patterns and updates the working tree. Use it after resolving local changes that blocked removal, or after editing .git/info/sparse-checkout by hand.
Choosing directories in practice
A reliable way to pick the cone is to start from what you build and test:
- Select the directory you work in.
- Try the build. If it fails because of a missing sibling, find which directory it needs, for example
libs/common. - Add that directory with
git sparse-checkout add libs/commonand try again. - Once the build passes, record the selection in a script or in the project documentation so teammates and CI use the same set.
Some teams keep a small helper script per service that runs git sparse-checkout set with the service directory and its known dependencies. That moves the dependency knowledge out of everyone's head and into version control.
Exercise
A repository has README.md at the root, apps/web/index.js, apps/mobile/main.kt, apps/CODEOWNERS, and tools/lint/run.sh. You run git sparse-checkout set --cone apps/web. Which of these five files are on disk, and why?
Show answer
Three files are on disk: README.md, apps/CODEOWNERS, and apps/web/index.js. README.md is a root file (rule 1). apps/CODEOWNERS is a file directly in apps/, which is an ancestor of the selected directory (rule 2). apps/web/index.js is inside the selected directory (rule 3). apps/mobile/main.kt is excluded because apps/mobile is neither selected nor an ancestor of a selected directory. tools/lint/run.sh is excluded because tools is not an ancestor of apps/web.
Exercise
You have selected services/api and now run git sparse-checkout set libs/common. What happens to services/api, and which command should you have used?
Show answer
set replaces the whole selection, so services/api is removed from disk and only the libs/common cone (plus root files and ancestor files) remains. To keep both, use git sparse-checkout add libs/common, or run set again with both directories: git sparse-checkout set services/api libs/common.