1GIT-SPARSE-CHECKOUT(1) Git Manual GIT-SPARSE-CHECKOUT(1)
2
3
4
6 git-sparse-checkout - Reduce your working tree to a subset of tracked
7 files
8
10 git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<options>]
11
13 This command is used to create sparse checkouts, which change the
14 working tree from having all tracked files present to only having a
15 subset of those files. It can also switch which subset of files are
16 present, or undo and go back to having all tracked files present in the
17 working copy.
18
19 The subset of files is chosen by providing a list of directories in
20 cone mode (the default), or by providing a list of patterns in non-cone
21 mode.
22
23 When in a sparse-checkout, other Git commands behave a bit differently.
24 For example, switching branches will not update paths outside the
25 sparse-checkout directories/patterns, and git commit -a will not record
26 paths outside the sparse-checkout directories/patterns as deleted.
27
28 THIS COMMAND IS EXPERIMENTAL. ITS BEHAVIOR, AND THE BEHAVIOR OF OTHER
29 COMMANDS IN THE PRESENCE OF SPARSE-CHECKOUTS, WILL LIKELY CHANGE IN THE
30 FUTURE.
31
33 list
34 Describe the directories or patterns in the sparse-checkout file.
35
36 set
37 Enable the necessary sparse-checkout config settings
38 (core.sparseCheckout, core.sparseCheckoutCone, and index.sparse) if
39 they are not already set to the desired values, populate the
40 sparse-checkout file from the list of arguments following the set
41 subcommand, and update the working directory to match.
42
43 To ensure that adjusting the sparse-checkout settings within a
44 worktree does not alter the sparse-checkout settings in other
45 worktrees, the set subcommand will upgrade your repository config
46 to use worktree-specific config if not already present. The
47 sparsity defined by the arguments to the set subcommand are stored
48 in the worktree-specific sparse-checkout file. See git-worktree(1)
49 and the documentation of extensions.worktreeConfig in git-config(1)
50 for more details.
51
52 When the --stdin option is provided, the directories or patterns
53 are read from standard in as a newline-delimited list instead of
54 from the arguments.
55
56 By default, the input list is considered a list of directories,
57 matching the output of git ls-tree -d --name-only. This includes
58 interpreting pathnames that begin with a double quote (") as
59 C-style quoted strings. Note that all files under the specified
60 directories (at any depth) will be included in the sparse checkout,
61 as well as files that are siblings of either the given directory or
62 any of its ancestors (see CONE PATTERN SET below for more details).
63 In the past, this was not the default, and --cone needed to be
64 specified or core.sparseCheckoutCone needed to be enabled.
65
66 When --no-cone is passed, the input list is considered a list of
67 patterns. This mode has a number of drawbacks, including not
68 working with some options like --sparse-index. As explained in the
69 "Non-cone Problems" section below, we do not recommend using it.
70
71 Use the --[no-]sparse-index option to use a sparse index (the
72 default is to not use it). A sparse index reduces the size of the
73 index to be more closely aligned with your sparse-checkout
74 definition. This can have significant performance advantages for
75 commands such as git status or git add. This feature is still
76 experimental. Some commands might be slower with a sparse index
77 until they are properly integrated with the feature.
78
79 WARNING: Using a sparse index requires modifying the index in a way
80 that is not completely understood by external tools. If you have
81 trouble with this compatibility, then run git sparse-checkout init
82 --no-sparse-index to rewrite your index to not be sparse. Older
83 versions of Git will not understand the sparse directory entries
84 index extension and may fail to interact with your repository until
85 it is disabled.
86
87 add
88 Update the sparse-checkout file to include additional directories
89 (in cone mode) or patterns (in non-cone mode). By default, these
90 directories or patterns are read from the command-line arguments,
91 but they can be read from stdin using the --stdin option.
92
93 reapply
94 Reapply the sparsity pattern rules to paths in the working tree.
95 Commands like merge or rebase can materialize paths to do their
96 work (e.g. in order to show you a conflict), and other
97 sparse-checkout commands might fail to sparsify an individual file
98 (e.g. because it has unstaged changes or conflicts). In such cases,
99 it can make sense to run git sparse-checkout reapply later after
100 cleaning up affected paths (e.g. resolving conflicts, undoing or
101 committing changes, etc.).
102
103 The reapply command can also take --[no-]cone and
104 --[no-]sparse-index flags, with the same meaning as the flags from
105 the set command, in order to change which sparsity mode you are
106 using without needing to also respecify all sparsity paths.
107
108 disable
109 Disable the core.sparseCheckout config setting, and restore the
110 working directory to include all files.
111
112 init
113 Deprecated command that behaves like set with no specified paths.
114 May be removed in the future.
115
116 Historically, set did not handle all the necessary config settings,
117 which meant that both init and set had to be called. Invoking both
118 meant the init step would first remove nearly all tracked files
119 (and in cone mode, ignored files too), then the set step would add
120 many of the tracked files (but not ignored files) back. In addition
121 to the lost files, the performance and UI of this combination was
122 poor.
123
124 Also, historically, init would not actually initialize the
125 sparse-checkout file if it already existed. This meant it was
126 possible to return to a sparse-checkout without remembering which
127 paths to pass to a subsequent set or add command. However, --cone
128 and --sparse-index options would not be remembered across the
129 disable command, so the easy restore of calling a plain init
130 decreased in utility.
131
132 check-rules
133 Check whether sparsity rules match one or more paths.
134
135 By default check-rules reads a list of paths from stdin and outputs
136 only the ones that match the current sparsity rules. The input is
137 expected to consist of one path per line, matching the output of
138 git ls-tree --name-only including that pathnames that begin with a
139 double quote (") are interpreted as C-style quoted strings.
140
141 When called with the --rules-file <file> flag the input files are
142 matched against the sparse checkout rules found in <file> instead
143 of the current ones. The rules in the files are expected to be in
144 the same form as accepted by git sparse-checkout set --stdin (in
145 particular, they must be newline-delimited).
146
147 By default, the rules passed to the --rules-file option are
148 interpreted as cone mode directories. To pass non-cone mode
149 patterns with --rules-file, combine the option with the --no-cone
150 option.
151
152 When called with the -z flag, the format of the paths input on
153 stdin as well as the output paths are \0 terminated and not quoted.
154 Note that this does not apply to the format of the rules passed
155 with the --rules-file option.
156
158 git sparse-checkout set MY/DIR1 SUB/DIR2
159 Change to a sparse checkout with all files (at any depth) under
160 MY/DIR1/ and SUB/DIR2/ present in the working copy (plus all files
161 immediately under MY/ and SUB/ and the toplevel directory). If
162 already in a sparse checkout, change which files are present in the
163 working copy to this new selection. Note that this command will
164 also delete all ignored files in any directory that no longer has
165 either tracked or non-ignored-untracked files present.
166
167 git sparse-checkout disable
168 Repopulate the working directory with all files, disabling sparse
169 checkouts.
170
171 git sparse-checkout add SOME/DIR/ECTORY
172 Add all files under SOME/DIR/ECTORY/ (at any depth) to the sparse
173 checkout, as well as all files immediately under SOME/DIR/ and
174 immediately under SOME/. Must already be in a sparse checkout
175 before using this command.
176
177 git sparse-checkout reapply
178 It is possible for commands to update the working tree in a way
179 that does not respect the selected sparsity directories. This can
180 come from tools external to Git writing files, or even affect Git
181 commands because of either special cases (such as hitting conflicts
182 when merging/rebasing), or because some commands didn’t fully
183 support sparse checkouts (e.g. the old recursive merge backend had
184 only limited support). This command reapplies the existing sparse
185 directory specifications to make the working directory match.
186
188 "Sparse checkout" allows populating the working directory sparsely. It
189 uses the skip-worktree bit (see git-update-index(1)) to tell Git
190 whether a file in the working directory is worth looking at. If the
191 skip-worktree bit is set, and the file is not present in the working
192 tree, then its absence is ignored. Git will avoid populating the
193 contents of those files, which makes a sparse checkout helpful when
194 working in a repository with many files, but only a few are important
195 to the current user.
196
197 The $GIT_DIR/info/sparse-checkout file is used to define the
198 skip-worktree reference bitmap. When Git updates the working directory,
199 it updates the skip-worktree bits in the index based on this file. The
200 files matching the patterns in the file will appear in the working
201 directory, and the rest will not.
202
204 The $GIT_DIR/info/sparse-checkout file populated by the set and add
205 subcommands is defined to be a bunch of patterns (one per line) using
206 the same syntax as .gitignore files. In cone mode, these patterns are
207 restricted to matching directories (and users only ever need supply or
208 see directory names), while in non-cone mode any gitignore-style
209 pattern is permitted. Using the full gitignore-style patterns in
210 non-cone mode has a number of shortcomings:
211
212 • Fundamentally, it makes various worktree-updating processes (pull,
213 merge, rebase, switch, reset, checkout, etc.) require O(N*M)
214 pattern matches, where N is the number of patterns and M is the
215 number of paths in the index. This scales poorly.
216
217 • Avoiding the scaling issue has to be done via limiting the number
218 of patterns via specifying leading directory name or glob.
219
220 • Passing globs on the command line is error-prone as users may
221 forget to quote the glob, causing the shell to expand it into all
222 matching files and pass them all individually along to
223 sparse-checkout set/add. While this could also be a problem with
224 e.g. "git grep — *.c", mistakes with grep/log/status appear in the
225 immediate output. With sparse-checkout, the mistake gets recorded
226 at the time the sparse-checkout command is run and might not be
227 problematic until the user later switches branches or rebases or
228 merges, thus putting a delay between the user’s error and when they
229 have a chance to catch/notice it.
230
231 • Related to the previous item, sparse-checkout has an add subcommand
232 but no remove subcommand. Even if a remove subcommand were added,
233 undoing an accidental unquoted glob runs the risk of "removing too
234 much", as it may remove entries that had been included before the
235 accidental add.
236
237 • Non-cone mode uses gitignore-style patterns to select what to
238 include (with the exception of negated patterns), while .gitignore
239 files use gitignore-style patterns to select what to exclude (with
240 the exception of negated patterns). The documentation on
241 gitignore-style patterns usually does not talk in terms of matching
242 or non-matching, but on what the user wants to "exclude". This can
243 cause confusion for users trying to learn how to specify
244 sparse-checkout patterns to get their desired behavior.
245
246 • Every other git subcommand that wants to provide "special path
247 pattern matching" of some sort uses pathspecs, but non-cone mode
248 for sparse-checkout uses gitignore patterns, which feels
249 inconsistent.
250
251 • It has edge cases where the "right" behavior is unclear. Two
252 examples:
253
254 First, two users are in a subdirectory, and the first runs
255 git sparse-checkout set '/toplevel-dir/*.c'
256 while the second runs
257 git sparse-checkout set relative-dir
258 Should those arguments be transliterated into
259 current/subdirectory/toplevel-dir/*.c
260 and
261 current/subdirectory/relative-dir
262 before inserting into the sparse-checkout file? The user who typed
263 the first command is probably aware that arguments to set/add are
264 supposed to be patterns in non-cone mode, and probably would not be
265 happy with such a transliteration. However, many gitignore-style
266 patterns are just paths, which might be what the user who typed the
267 second command was thinking, and they'd be upset if their argument
268 wasn't transliterated.
269
270 Second, what should bash-completion complete on for set/add commands
271 for non-cone users? If it suggests paths, is it exacerbating the
272 problem above? Also, if it suggests paths, what if the user has a
273 file or directory that begins with either a '!' or '#' or has a '*',
274 '\', '?', '[', or ']' in its name? And if it suggests paths, will
275 it complete "/pro" to "/proc" (in the root filesystem) rather than to
276 "/progress.txt" in the current directory? (Note that users are
277 likely to want to start paths with a leading '/' in non-cone mode,
278 for the same reason that .gitignore files often have one.)
279 Completing on files or directories might give nasty surprises in
280 all these cases.
281
282 • The excessive flexibility made other extensions essentially
283 impractical. --sparse-index is likely impossible in non-cone mode;
284 even if it is somehow feasible, it would have been far more work to
285 implement and may have been too slow in practice. Some ideas for
286 adding coupling between partial clones and sparse checkouts are
287 only practical with a more restricted set of paths as well.
288
289 For all these reasons, non-cone mode is deprecated. Please switch to
290 using cone mode.
291
293 The "cone mode", which is the default, lets you specify only what
294 directories to include. For any directory specified, all paths below
295 that directory will be included, and any paths immediately under
296 leading directories (including the toplevel directory) will also be
297 included. Thus, if you specified the directory Documentation/technical/
298 then your sparse checkout would contain:
299
300 • all files in the toplevel-directory
301
302 • all files immediately under Documentation/
303
304 • all files at any depth under Documentation/technical/
305
306 Also, in cone mode, even if no directories are specified, then the
307 files in the toplevel directory will be included.
308
309 When changing the sparse-checkout patterns in cone mode, Git will
310 inspect each tracked directory that is not within the sparse-checkout
311 cone to see if it contains any untracked files. If all of those files
312 are ignored due to the .gitignore patterns, then the directory will be
313 deleted. If any of the untracked files within that directory is not
314 ignored, then no deletions will occur within that directory and a
315 warning message will appear. If these files are important, then reset
316 your sparse-checkout definition so they are included, use git add and
317 git commit to store them, then remove any remaining files manually to
318 ensure Git can behave optimally.
319
320 See also the "Internals — Cone Pattern Set" section to learn how the
321 directories are transformed under the hood into a subset of the Full
322 Pattern Set of sparse-checkout.
323
325 The full pattern set allows for arbitrary pattern matches and
326 complicated inclusion/exclusion rules. These can result in O(N*M)
327 pattern matches when updating the index, where N is the number of
328 patterns and M is the number of paths in the index. To combat this
329 performance issue, a more restricted pattern set is allowed when
330 core.sparseCheckoutCone is enabled.
331
332 The sparse-checkout file uses the same syntax as .gitignore files; see
333 gitignore(5) for details. Here, though, the patterns are usually being
334 used to select which files to include rather than which files to
335 exclude. (However, it can get a bit confusing since gitignore-style
336 patterns have negations defined by patterns which begin with a !, so
337 you can also select files to not include.)
338
339 For example, to select everything, and then to remove the file unwanted
340 (so that every file will appear in your working tree except the file
341 named unwanted):
342
343 git sparse-checkout set --no-cone '/*' '!unwanted'
344
345 These patterns are just placed into the $GIT_DIR/info/sparse-checkout
346 as-is, so the contents of that file at this point would be
347
348 /*
349 !unwanted
350
351 See also the "Sparse Checkout" section of git-read-tree(1) to learn
352 more about the gitignore-style patterns used in sparse checkouts.
353
355 In cone mode, only directories are accepted, but they are translated
356 into the same gitignore-style patterns used in the full pattern set. We
357 refer to the particular patterns used in those mode as being of one of
358 two types:
359
360 1. Recursive: All paths inside a directory are included.
361
362 2. Parent: All files immediately inside a directory are included.
363
364 Since cone mode always includes files at the toplevel, when running git
365 sparse-checkout set with no directories specified, the toplevel
366 directory is added as a parent pattern. At this point, the
367 sparse-checkout file contains the following patterns:
368
369 /*
370 !/*/
371
372 This says "include everything immediately under the toplevel directory,
373 but nothing at any level below that."
374
375 When in cone mode, the git sparse-checkout set subcommand takes a list
376 of directories. The command git sparse-checkout set A/B/C sets the
377 directory A/B/C as a recursive pattern, the directories A and A/B are
378 added as parent patterns. The resulting sparse-checkout file is now
379
380 /*
381 !/*/
382 /A/
383 !/A/*/
384 /A/B/
385 !/A/B/*/
386 /A/B/C/
387
388 Here, order matters, so the negative patterns are overridden by the
389 positive patterns that appear lower in the file.
390
391 Unless core.sparseCheckoutCone is explicitly set to false, Git will
392 parse the sparse-checkout file expecting patterns of these types. Git
393 will warn if the patterns do not match. If the patterns do match the
394 expected format, then Git will use faster hash-based algorithms to
395 compute inclusion in the sparse-checkout. If they do not match, git
396 will behave as though core.sparseCheckoutCone was false, regardless of
397 its setting.
398
399 In the cone mode case, despite the fact that full patterns are written
400 to the $GIT_DIR/info/sparse-checkout file, the git sparse-checkout list
401 subcommand will list the directories that define the recursive
402 patterns. For the example sparse-checkout file above, the output is as
403 follows:
404
405 $ git sparse-checkout list
406 A/B/C
407
408 If core.ignoreCase=true, then the pattern-matching algorithm will use a
409 case-insensitive check. This corrects for case mismatched filenames in
410 the git sparse-checkout set command to reflect the expected cone in the
411 working directory.
412
414 If your repository contains one or more submodules, then submodules are
415 populated based on interactions with the git submodule command.
416 Specifically, git submodule init -- <path> will ensure the submodule at
417 <path> is present, while git submodule deinit [-f] -- <path> will
418 remove the files for the submodule at <path> (including any untracked
419 files, uncommitted changes, and unpushed history). Similar to how
420 sparse-checkout removes files from the working tree but still leaves
421 entries in the index, deinitialized submodules are removed from the
422 working directory but still have an entry in the index.
423
424 Since submodules may have unpushed changes or untracked files, removing
425 them could result in data loss. Thus, changing sparse
426 inclusion/exclusion rules will not cause an already checked out
427 submodule to be removed from the working copy. Said another way, just
428 as checkout will not cause submodules to be automatically removed or
429 initialized even when switching between branches that remove or add
430 submodules, using sparse-checkout to reduce or expand the scope of
431 "interesting" files will not cause submodules to be automatically
432 deinitialized or initialized either.
433
434 Further, the above facts mean that there are multiple reasons that
435 "tracked" files might not be present in the working copy: sparsity
436 pattern application from sparse-checkout, and submodule initialization
437 state. Thus, commands like git grep that work on tracked files in the
438 working copy may return results that are limited by either or both of
439 these restrictions.
440
442 git-read-tree(1) gitignore(5)
443
445 Part of the git(1) suite
446
447
448
449Git 2.43.0 11/20/2023 GIT-SPARSE-CHECKOUT(1)