Sub topic 4
Cone Mode: Choosing Directories
This chapter assumes you can clone, commit, and branch in Git, that you are comfortable in a terminal, and that you know the difference between the repository (history) and the working tree (files on disk). You should also have seen git sparse-checkout once.
What cone mode is
Cone mode is the default sparse checkout mode in current Git versions. Instead of arbitrary patterns, you name directories. Git then checks out:
- every file directly in the repository root,
- every file directly in each parent directory of a chosen directory,
- everything, recursively, inside each chosen directory.
Because the rules are this restricted, Git can match paths quickly. That is why cone mode is recommended for large monorepos.
Selecting directories
Enable cone mode and pick a directory in one command:
git sparse-checkout set --cone services/api
cat .git/info/sparse-checkoutThe stored file uses a fixed format:
/*
!/*/
/services/
!/services/*/
/services/api/Read it top to bottom:
/*includes all files at the root.!/*/excludes every directory at the root./services/re-includes theservicesdirectory, but only its direct files.!/services/*/excludes its subdirectories./services/api/includes the chosen directory with everything below it.
You do not write these lines yourself. Git generates them from the directory names you give.
Choosing more than one directory
set takes any number of directories and replaces the previous selection:
git sparse-checkout set services/api libs/commonBoth directories are checked out in full. Their parent directories (services/ and libs/) contribute only their direct files. Siblings such as services/billing stay absent.
What you will see on disk
After set, Git removes files outside the cone from the working tree and writes the ones inside it. Run ls at the root and you will see root files plus only the parent directories that lead to your selection. Nothing changes in history. git log and git branch still show everything, and you can switch to a branch that touches excluded directories.
Common errors
- Passing a file path. Cone mode takes directories. A path such as
services/api/main.gois not a valid entry, so select its directory instead. - Expecting a trailing-slash or glob syntax. In cone mode,
services/*is not a pattern. Writeservicesto get the whole directory. - Forgetting the parent files. Files in the root and in parent directories always appear. If you see unexpected files in
services/, that is cone behavior, not a bug. - Replacing instead of adding. Running
seta second time discards your earlier selection. Useaddwhen you want to extend it. Chapter 5 coversadd,list, anddisable.
Why the restriction helps
In pattern mode, Git may have to test every path against every pattern. In cone mode it compares directory names against a small set, so the cost grows with the number of chosen directories, not the number of files. The same structure also enables the sparse index, which a later chapter covers.
Exercises
Exercise
You run git sparse-checkout set --cone libs/common/utils. Is a file at libs/README.md in your working tree? What about libs/common/logger/log.go?
Show answer
libs/README.md is present, because files directly in a parent directory (libs/) of a chosen directory are included. libs/common/logger/log.go is absent, because libs/common/logger is a sibling of the chosen utils directory, and only direct files of libs/common are included, not its other subdirectories.
Exercise
You ran git sparse-checkout set --cone services/api and later ran git sparse-checkout set --cone libs/common. Which directories are now checked out in full, and what command would you have used to keep both?
Show answer
Only libs/common is checked out in full, because the second set replaced the first selection. To keep both, run git sparse-checkout add libs/common (or pass both directories to a single set).