git sparse-checkout <subcommand> [options]
THIS COMMAND IS EXPERIMENTAL. ITS BEHAVIOR, AND THE BEHAVIOR OF OTHER COMMANDS IN THE PRESENCE OF SPARSE-CHECKOUTS, WILL LIKELY CHANGE IN THE FUTURE.
To avoid interfering with other worktrees, it first enables the extensions.worktreeConfig setting and makes sure to set the core.sparseCheckout setting in the worktree-specific config file.
When --cone is provided, the core.sparseCheckoutCone setting is also set, allowing for better performance with a limited set of patterns (see CONE PATTERN SET below).
Use the --[no-]sparse-index option to toggle the use of the sparse index format. This reduces the size of the index to be more closely aligned with your sparse-checkout definition. This can have significant performance advantages for commands such as git status or git add. This feature is still experimental. Some commands might be slower with a sparse index until they are properly integrated with the feature.
WARNING: Using a sparse index requires modifying the index in a way that is not completely understood by external tools. If you have trouble with this compatibility, then run git sparse-checkout init --no-sparse-index to rewrite your index to not be sparse. Older versions of Git will not understand the sparse directory entries index extension and may fail to interact with your repository until it is disabled.
When the --stdin option is provided, the patterns are read from standard in as a newline-delimited list instead of from the arguments.
When core.sparseCheckoutCone is enabled, the input list is considered a list of directories instead of sparse-checkout patterns. The command writes patterns to the sparse-checkout file to include all files contained in those directories (recursively) as well as files that are siblings of ancestor directories. The input format matches the output of git ls-tree --name-only. This includes interpreting pathnames that begin with a double quote (") as C-style quoted strings.
The $GIT_DIR/info/sparse-checkout file is used to define the skip-worktree reference bitmap. When Git updates the working directory, it updates the skip-worktree bits in the index based on this file. The files matching the patterns in the file will appear in the working directory, and the rest will not.
To enable the sparse-checkout feature, run git sparse-checkout init to initialize a simple sparse-checkout file and enable the core.sparseCheckout config setting. Then, run git sparse-checkout set to modify the patterns in the sparse-checkout file.
To repopulate the working directory with all files, use the git sparse-checkout disable command.
While $GIT_DIR/info/sparse-checkout is usually used to specify what files are included, you can also specify what files are not included, using negative patterns. For example, to remove the file unwanted:
The accepted patterns in the cone pattern set are:
In addition to the above two patterns, we also expect that all files in the root directory are included. If a recursive pattern is added, then all leading directories are added as parent patterns.
By default, when running git sparse-checkout init, the root directory is added as a parent pattern. At this point, the sparse-checkout file contains the following patterns:
This says "include everything in root, but nothing two levels below root."
When in cone mode, the git sparse-checkout set subcommand takes a list of directories instead of a list of sparse-checkout patterns. In this mode, the command git sparse-checkout set A/B/C sets the directory A/B/C as a recursive pattern, the directories A and A/B are added as parent patterns. The resulting sparse-checkout file is now
/* !/*/ /A/ !/A/*/ /A/B/ !/A/B/*/ /A/B/C/
Here, order matters, so the negative patterns are overridden by the positive patterns that appear lower in the file.
If core.sparseCheckoutCone=true, then Git will parse the sparse-checkout file expecting patterns of these types. Git will warn if the patterns do not match. If the patterns do match the expected format, then Git will use faster hash- based algorithms to compute inclusion in the sparse-checkout.
In the cone mode case, the git sparse-checkout list subcommand will list the directories that define the recursive patterns. For the example sparse-checkout file above, the output is as follows:
$ git sparse-checkout list A/B/C
If core.ignoreCase=true, then the pattern-matching algorithm will use a case-insensitive check. This corrects for case mismatched filenames in the git sparse-checkout set command to reflect the expected cone in the working directory.
Since submodules may have unpushed changes or untracked files, removing them could result in data loss. Thus, changing sparse inclusion/exclusion rules will not cause an already checked out submodule to be removed from the working copy. Said another way, just as checkout will not cause submodules to be automatically removed or initialized even when switching between branches that remove or add submodules, using sparse-checkout to reduce or expand the scope of "interesting" files will not cause submodules to be automatically deinitialized or initialized either.
Further, the above facts mean that there are multiple reasons that "tracked" files might not be present in the working copy: sparsity pattern application from sparse-checkout, and submodule initialization state. Thus, commands like git grep that work on tracked files in the working copy may return results that are limited by either or both of these restrictions.