2 .\" Title: git-sparse-checkout
3 .\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
7 .\" Source: Git 2.39.1.231.ga7caae2729
10 .TH "GIT\-SPARSE\-CHECKOU" "1" "01/17/2023" "Git 2\&.39\&.1\&.231\&.ga7caae" "Git Manual"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 git-sparse-checkout \- Reduce your working tree to a subset of tracked files
35 \fIgit sparse\-checkout\fR (init | list | set | add | reapply | disable) [<options>]
40 This command is used to create sparse checkouts, which change the working tree from having all tracked files present to only having a subset of those files\&. It can also switch which subset of files are present, or undo and go back to having all tracked files present in the working copy\&.
42 The subset of files is chosen by providing a list of directories in cone mode (the default), or by providing a list of patterns in non\-cone mode\&.
44 When in a sparse\-checkout, other Git commands behave a bit differently\&. For example, switching branches will not update paths outside the sparse\-checkout directories/patterns, and \fBgit commit \-a\fR will not record paths outside the sparse\-checkout directories/patterns as deleted\&.
46 THIS COMMAND IS EXPERIMENTAL\&. ITS BEHAVIOR, AND THE BEHAVIOR OF OTHER COMMANDS IN THE PRESENCE OF SPARSE\-CHECKOUTS, WILL LIKELY CHANGE IN THE FUTURE\&.
51 Describe the directories or patterns in the sparse\-checkout file\&.
56 Enable the necessary sparse\-checkout config settings (\fBcore\&.sparseCheckout\fR,
57 \fBcore\&.sparseCheckoutCone\fR, and
58 \fBindex\&.sparse\fR) if they are not already set to the desired values, populate the sparse\-checkout file from the list of arguments following the
60 subcommand, and update the working directory to match\&.
62 To ensure that adjusting the sparse\-checkout settings within a worktree does not alter the sparse\-checkout settings in other worktrees, the
64 subcommand will upgrade your repository config to use worktree\-specific config if not already present\&. The sparsity defined by the arguments to the
66 subcommand are stored in the worktree\-specific sparse\-checkout file\&. See
68 and the documentation of
69 \fBextensions\&.worktreeConfig\fR
76 option is provided, the directories or patterns are read from standard in as a newline\-delimited list instead of from the arguments\&.
78 By default, the input list is considered a list of directories, matching the output of
79 \fBgit ls\-tree \-d \-\-name\-only\fR\&. This includes interpreting pathnames that begin with a double quote (") as C\-style quoted strings\&. Note that all files under the specified directories (at any depth) will be included in the sparse checkout, as well as files that are siblings of either the given directory or any of its ancestors (see
80 \fICONE PATTERN SET\fR
81 below for more details)\&. In the past, this was not the default, and
83 needed to be specified or
84 \fBcore\&.sparseCheckoutCone\fR
85 needed to be enabled\&.
89 is passed, the input list is considered a list of patterns\&. This mode has a number of drawbacks, including not working with some options like
90 \fB\-\-sparse\-index\fR\&. As explained in the "Non\-cone Problems" section below, we do not recommend using it\&.
93 \fB\-\-[no\-]sparse\-index\fR
94 option to use a sparse index (the default is to not use it)\&. A sparse index 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
97 \fBgit add\fR\&. This feature is still experimental\&. Some commands might be slower with a sparse index until they are properly integrated with the feature\&.
100 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
101 \fBgit sparse\-checkout init \-\-no\-sparse\-index\fR
102 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\&.
107 Update the sparse\-checkout file to include additional directories (in cone mode) or patterns (in non\-cone mode)\&. By default, these directories or patterns are read from the command\-line arguments, but they can be read from stdin using the
114 Reapply the sparsity pattern rules to paths in the working tree\&. Commands like merge or rebase can materialize paths to do their work (e\&.g\&. in order to show you a conflict), and other sparse\-checkout commands might fail to sparsify an individual file (e\&.g\&. because it has unstaged changes or conflicts)\&. In such cases, it can make sense to run
115 \fBgit sparse\-checkout reapply\fR
116 later after cleaning up affected paths (e\&.g\&. resolving conflicts, undoing or committing changes, etc\&.)\&.
120 command can also take
123 \fB\-\-[no\-]sparse\-index\fR
124 flags, with the same meaning as the flags from the
126 command, in order to change which sparsity mode you are using without needing to also respecify all sparsity paths\&.
132 \fBcore\&.sparseCheckout\fR
133 config setting, and restore the working directory to include all files\&.
138 Deprecated command that behaves like
140 with no specified paths\&. May be removed in the future\&.
144 did not handle all the necessary config settings, which meant that both
148 had to be called\&. Invoking both meant the
150 step would first remove nearly all tracked files (and in cone mode, ignored files too), then the
152 step would add many of the tracked files (but not ignored files) back\&. In addition to the lost files, the performance and UI of this combination was poor\&.
156 would not actually initialize the sparse\-checkout file if it already existed\&. This meant it was possible to return to a sparse\-checkout without remembering which paths to pass to a subsequent
163 \fB\-\-sparse\-index\fR
164 options would not be remembered across the disable command, so the easy restore of calling a plain
166 decreased in utility\&.
170 \fBgit sparse\-checkout set MY/DIR1 SUB/DIR2\fR
172 Change to a sparse checkout with all files (at any depth) under MY/DIR1/ and SUB/DIR2/ present in the working copy (plus all files immediately under MY/ and SUB/ and the toplevel directory)\&. If already in a sparse checkout, change which files are present in the working copy to this new selection\&. Note that this command will also delete all ignored files in any directory that no longer has either tracked or non\-ignored\-untracked files present\&.
175 \fBgit sparse\-checkout disable\fR
177 Repopulate the working directory with all files, disabling sparse checkouts\&.
180 \fBgit sparse\-checkout add SOME/DIR/ECTORY\fR
182 Add all files under SOME/DIR/ECTORY/ (at any depth) to the sparse checkout, as well as all files immediately under SOME/DIR/ and immediately under SOME/\&. Must already be in a sparse checkout before using this command\&.
185 \fBgit sparse\-checkout reapply\fR
187 It is possible for commands to update the working tree in a way that does not respect the selected sparsity directories\&. This can come from tools external to Git writing files, or even affect Git commands because of either special cases (such as hitting conflicts when merging/rebasing), or because some commands didn\(cqt fully support sparse checkouts (e\&.g\&. the old
189 merge backend had only limited support)\&. This command reapplies the existing sparse directory specifications to make the working directory match\&.
191 .SH "INTERNALS \(em SPARSE CHECKOUT"
193 "Sparse checkout" allows populating the working directory sparsely\&. It uses the skip\-worktree bit (see \fBgit-update-index\fR(1)) to tell Git whether a file in the working directory is worth looking at\&. If the skip\-worktree bit is set, and the file is not present in the working tree, then its absence is ignored\&. Git will avoid populating the contents of those files, which makes a sparse checkout helpful when working in a repository with many files, but only a few are important to the current user\&.
195 The \fB$GIT_DIR/info/sparse\-checkout\fR 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\&.
196 .SH "INTERNALS \(em NON\-CONE PROBLEMS"
198 The \fB$GIT_DIR/info/sparse\-checkout\fR file populated by the \fBset\fR and \fBadd\fR subcommands is defined to be a bunch of patterns (one per line) using the same syntax as \fB\&.gitignore\fR files\&. In cone mode, these patterns are restricted to matching directories (and users only ever need supply or see directory names), while in non\-cone mode any gitignore\-style pattern is permitted\&. Using the full gitignore\-style patterns in non\-cone mode has a number of shortcomings:
208 Fundamentally, it makes various worktree\-updating processes (pull, merge, rebase, switch, reset, checkout, etc\&.) require O(N*M) pattern matches, where N is the number of patterns and M is the number of paths in the index\&. This scales poorly\&.
219 Avoiding the scaling issue has to be done via limiting the number of patterns via specifying leading directory name or glob\&.
230 Passing globs on the command line is error\-prone as users may forget to quote the glob, causing the shell to expand it into all matching files and pass them all individually along to sparse\-checkout set/add\&. While this could also be a problem with e\&.g\&. "git grep \(em *\&.c", mistakes with grep/log/status appear in the immediate output\&. With sparse\-checkout, the mistake gets recorded at the time the sparse\-checkout command is run and might not be problematic until the user later switches branches or rebases or merges, thus putting a delay between the user\(cqs error and when they have a chance to catch/notice it\&.
241 Related to the previous item, sparse\-checkout has an
245 subcommand\&. Even if a
247 subcommand were added, undoing an accidental unquoted glob runs the risk of "removing too much", as it may remove entries that had been included before the accidental add\&.
258 Non\-cone mode uses gitignore\-style patterns to select what to
260 (with the exception of negated patterns), while \&.gitignore files use gitignore\-style patterns to select what to
262 (with the exception of negated patterns)\&. The documentation on gitignore\-style patterns usually does not talk in terms of matching or non\-matching, but on what the user wants to "exclude"\&. This can cause confusion for users trying to learn how to specify sparse\-checkout patterns to get their desired behavior\&.
273 Every other git subcommand that wants to provide "special path pattern matching" of some sort uses pathspecs, but non\-cone mode for sparse\-checkout uses gitignore patterns, which feels inconsistent\&.
284 It has edge cases where the "right" behavior is unclear\&. Two examples:
290 First, two users are in a subdirectory, and the first runs
291 git sparse\-checkout set \(aq/toplevel\-dir/*\&.c\(aq
292 while the second runs
293 git sparse\-checkout set relative\-dir
294 Should those arguments be transliterated into
295 current/subdirectory/toplevel\-dir/*\&.c
297 current/subdirectory/relative\-dir
298 before inserting into the sparse\-checkout file? The user who typed
299 the first command is probably aware that arguments to set/add are
300 supposed to be patterns in non\-cone mode, and probably would not be
301 happy with such a transliteration\&. However, many gitignore\-style
302 patterns are just paths, which might be what the user who typed the
303 second command was thinking, and they\(aqd be upset if their argument
304 wasn\(aqt transliterated\&.
314 Second, what should bash\-completion complete on for set/add commands
315 for non\-cone users? If it suggests paths, is it exacerbating the
316 problem above? Also, if it suggests paths, what if the user has a
317 file or directory that begins with either a \(aq!\(aq or \(aq#\(aq or has a \(aq*\(aq,
318 \(aq\e\(aq, \(aq?\(aq, \(aq[\(aq, or \(aq]\(aq in its name? And if it suggests paths, will
319 it complete "/pro" to "/proc" (in the root filesytem) rather than to
320 "/progress\&.txt" in the current directory? (Note that users are
321 likely to want to start paths with a leading \(aq/\(aq in non\-cone mode,
322 for the same reason that \&.gitignore files often have one\&.)
323 Completing on files or directories might give nasty surprises in
339 The excessive flexibility made other extensions essentially impractical\&.
340 \fB\-\-sparse\-index\fR
341 is likely impossible in non\-cone mode; even if it is somehow feasible, it would have been far more work to implement and may have been too slow in practice\&. Some ideas for adding coupling between partial clones and sparse checkouts are only practical with a more restricted set of paths as well\&.
344 For all these reasons, non\-cone mode is deprecated\&. Please switch to using cone mode\&.
345 .SH "INTERNALS \(em CONE MODE HANDLING"
347 The "cone mode", which is the default, lets you specify only what directories to include\&. For any directory specified, all paths below that directory will be included, and any paths immediately under leading directories (including the toplevel directory) will also be included\&. Thus, if you specified the directory Documentation/technical/ then your sparse checkout would contain:
357 all files in the toplevel\-directory
368 all files immediately under Documentation/
379 all files at any depth under Documentation/technical/
382 Also, in cone mode, even if no directories are specified, then the files in the toplevel directory will be included\&.
384 When changing the sparse\-checkout patterns in cone mode, Git will inspect each tracked directory that is not within the sparse\-checkout cone to see if it contains any untracked files\&. If all of those files are ignored due to the \fB\&.gitignore\fR patterns, then the directory will be deleted\&. If any of the untracked files within that directory is not ignored, then no deletions will occur within that directory and a warning message will appear\&. If these files are important, then reset your sparse\-checkout definition so they are included, use \fBgit add\fR and \fBgit commit\fR to store them, then remove any remaining files manually to ensure Git can behave optimally\&.
386 See also the "Internals \(em Cone Pattern Set" section to learn how the directories are transformed under the hood into a subset of the Full Pattern Set of sparse\-checkout\&.
387 .SH "INTERNALS \(em FULL PATTERN SET"
389 The full pattern set allows for arbitrary pattern matches and complicated inclusion/exclusion rules\&. These can result in O(N*M) pattern matches when updating the index, where N is the number of patterns and M is the number of paths in the index\&. To combat this performance issue, a more restricted pattern set is allowed when \fBcore\&.sparseCheckoutCone\fR is enabled\&.
391 The sparse\-checkout file uses the same syntax as \fB\&.gitignore\fR files; see \fBgitignore\fR(5) for details\&. Here, though, the patterns are usually being used to select which files to include rather than which files to exclude\&. (However, it can get a bit confusing since gitignore\-style patterns have negations defined by patterns which begin with a \fI!\fR, so you can also select files to \fInot\fR include\&.)
393 For example, to select everything, and then to remove the file \fBunwanted\fR (so that every file will appear in your working tree except the file named \fBunwanted\fR):
399 git sparse\-checkout set \-\-no\-cone \(aq/*\(aq \(aq!unwanted\(aq
405 These patterns are just placed into the \fB$GIT_DIR/info/sparse\-checkout\fR as\-is, so the contents of that file at this point would be
419 See also the "Sparse Checkout" section of \fBgit-read-tree\fR(1) to learn more about the gitignore\-style patterns used in sparse checkouts\&.
420 .SH "INTERNALS \(em CONE PATTERN SET"
422 In cone mode, only directories are accepted, but they are translated into the same gitignore\-style patterns used in the full pattern set\&. We refer to the particular patterns used in those mode as being of one of two types:
433 All paths inside a directory are included\&.
445 All files immediately inside a directory are included\&.
448 Since cone mode always includes files at the toplevel, when running \fBgit sparse\-checkout set\fR with no directories specified, the toplevel directory is added as a parent pattern\&. At this point, the sparse\-checkout file contains the following patterns:
462 This says "include everything immediately under the toplevel directory, but nothing at any level below that\&."
464 When in cone mode, the \fBgit sparse\-checkout set\fR subcommand takes a list of directories\&. The command \fBgit sparse\-checkout set A/B/C\fR sets the directory \fBA/B/C\fR as a recursive pattern, the directories \fBA\fR and \fBA/B\fR are added as parent patterns\&. The resulting sparse\-checkout file is now
483 Here, order matters, so the negative patterns are overridden by the positive patterns that appear lower in the file\&.
485 Unless \fBcore\&.sparseCheckoutCone\fR is explicitly set to \fBfalse\fR, 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\&. If they do not match, git will behave as though \fBcore\&.sparseCheckoutCone\fR was false, regardless of its setting\&.
487 In the cone mode case, despite the fact that full patterns are written to the $GIT_DIR/info/sparse\-checkout file, the \fBgit sparse\-checkout list\fR subcommand will list the directories that define the recursive patterns\&. For the example sparse\-checkout file above, the output is as follows:
493 $ git sparse\-checkout list
501 If \fBcore\&.ignoreCase=true\fR, then the pattern\-matching algorithm will use a case\-insensitive check\&. This corrects for case mismatched filenames in the \fIgit sparse\-checkout set\fR command to reflect the expected cone in the working directory\&.
502 .SH "INTERNALS \(em SUBMODULES"
504 If your repository contains one or more submodules, then submodules are populated based on interactions with the \fBgit submodule\fR command\&. Specifically, \fBgit submodule init \-\- <path>\fR will ensure the submodule at \fB<path>\fR is present, while \fBgit submodule deinit [\-f] \-\- <path>\fR will remove the files for the submodule at \fB<path>\fR (including any untracked files, uncommitted changes, and unpushed history)\&. Similar to how sparse\-checkout removes files from the working tree but still leaves entries in the index, deinitialized submodules are removed from the working directory but still have an entry in the index\&.
506 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 \fBcheckout\fR will not cause submodules to be automatically removed or initialized even when switching between branches that remove or add submodules, using \fBsparse\-checkout\fR to reduce or expand the scope of "interesting" files will not cause submodules to be automatically deinitialized or initialized either\&.
508 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 \fBgit grep\fR that work on tracked files in the working copy may return results that are limited by either or both of these restrictions\&.
511 \fBgit-read-tree\fR(1) \fBgitignore\fR(5)
514 Part of the \fBgit\fR(1) suite