1GIT-SPARSE-CHECKOU(1) Git Manual GIT-SPARSE-CHECKOU(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) [<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
133 git sparse-checkout set MY/DIR1 SUB/DIR2
134 Change to a sparse checkout with all files (at any depth) under
135 MY/DIR1/ and SUB/DIR2/ present in the working copy (plus all files
136 immediately under MY/ and SUB/ and the toplevel directory). If
137 already in a sparse checkout, change which files are present in the
138 working copy to this new selection. Note that this command will
139 also delete all ignored files in any directory that no longer has
140 either tracked or non-ignored-untracked files present.
141
142 git sparse-checkout disable
143 Repopulate the working directory with all files, disabling sparse
144 checkouts.
145
146 git sparse-checkout add SOME/DIR/ECTORY
147 Add all files under SOME/DIR/ECTORY/ (at any depth) to the sparse
148 checkout, as well as all files immediately under SOME/DIR/ and
149 immediately under SOME/. Must already be in a sparse checkout
150 before using this command.
151
152 git sparse-checkout reapply
153 It is possible for commands to update the working tree in a way
154 that does not respect the selected sparsity directories. This can
155 come from tools external to Git writing files, or even affect Git
156 commands because of either special cases (such as hitting conflicts
157 when merging/rebasing), or because some commands didn’t fully
158 support sparse checkouts (e.g. the old recursive merge backend had
159 only limited support). This command reapplies the existing sparse
160 directory specifications to make the working directory match.
161
163 "Sparse checkout" allows populating the working directory sparsely. It
164 uses the skip-worktree bit (see git-update-index(1)) to tell Git
165 whether a file in the working directory is worth looking at. If the
166 skip-worktree bit is set, and the file is not present in the working
167 tree, then its absence is ignored. Git will avoid populating the
168 contents of those files, which makes a sparse checkout helpful when
169 working in a repository with many files, but only a few are important
170 to the current user.
171
172 The $GIT_DIR/info/sparse-checkout file is used to define the
173 skip-worktree reference bitmap. When Git updates the working directory,
174 it updates the skip-worktree bits in the index based on this file. The
175 files matching the patterns in the file will appear in the working
176 directory, and the rest will not.
177
179 The $GIT_DIR/info/sparse-checkout file populated by the set and add
180 subcommands is defined to be a bunch of patterns (one per line) using
181 the same syntax as .gitignore files. In cone mode, these patterns are
182 restricted to matching directories (and users only ever need supply or
183 see directory names), while in non-cone mode any gitignore-style
184 pattern is permitted. Using the full gitignore-style patterns in
185 non-cone mode has a number of shortcomings:
186
187 • Fundamentally, it makes various worktree-updating processes (pull,
188 merge, rebase, switch, reset, checkout, etc.) require O(N*M)
189 pattern matches, where N is the number of patterns and M is the
190 number of paths in the index. This scales poorly.
191
192 • Avoiding the scaling issue has to be done via limiting the number
193 of patterns via specifying leading directory name or glob.
194
195 • Passing globs on the command line is error-prone as users may
196 forget to quote the glob, causing the shell to expand it into all
197 matching files and pass them all individually along to
198 sparse-checkout set/add. While this could also be a problem with
199 e.g. "git grep — *.c", mistakes with grep/log/status appear in the
200 immediate output. With sparse-checkout, the mistake gets recorded
201 at the time the sparse-checkout command is run and might not be
202 problematic until the user later switches branches or rebases or
203 merges, thus putting a delay between the user’s error and when they
204 have a chance to catch/notice it.
205
206 • Related to the previous item, sparse-checkout has an add subcommand
207 but no remove subcommand. Even if a remove subcommand were added,
208 undoing an accidental unquoted glob runs the risk of "removing too
209 much", as it may remove entries that had been included before the
210 accidental add.
211
212 • Non-cone mode uses gitignore-style patterns to select what to
213 include (with the exception of negated patterns), while .gitignore
214 files use gitignore-style patterns to select what to exclude (with
215 the exception of negated patterns). The documentation on
216 gitignore-style patterns usually does not talk in terms of matching
217 or non-matching, but on what the user wants to "exclude". This can
218 cause confusion for users trying to learn how to specify
219 sparse-checkout patterns to get their desired behavior.
220
221 • Every other git subcommand that wants to provide "special path
222 pattern matching" of some sort uses pathspecs, but non-cone mode
223 for sparse-checkout uses gitignore patterns, which feels
224 inconsistent.
225
226 • It has edge cases where the "right" behavior is unclear. Two
227 examples:
228
229 First, two users are in a subdirectory, and the first runs
230 git sparse-checkout set '/toplevel-dir/*.c'
231 while the second runs
232 git sparse-checkout set relative-dir
233 Should those arguments be transliterated into
234 current/subdirectory/toplevel-dir/*.c
235 and
236 current/subdirectory/relative-dir
237 before inserting into the sparse-checkout file? The user who typed
238 the first command is probably aware that arguments to set/add are
239 supposed to be patterns in non-cone mode, and probably would not be
240 happy with such a transliteration. However, many gitignore-style
241 patterns are just paths, which might be what the user who typed the
242 second command was thinking, and they'd be upset if their argument
243 wasn't transliterated.
244
245 Second, what should bash-completion complete on for set/add commands
246 for non-cone users? If it suggests paths, is it exacerbating the
247 problem above? Also, if it suggests paths, what if the user has a
248 file or directory that begins with either a '!' or '#' or has a '*',
249 '\', '?', '[', or ']' in its name? And if it suggests paths, will
250 it complete "/pro" to "/proc" (in the root filesytem) rather than to
251 "/progress.txt" in the current directory? (Note that users are
252 likely to want to start paths with a leading '/' in non-cone mode,
253 for the same reason that .gitignore files often have one.)
254 Completing on files or directories might give nasty surprises in
255 all these cases.
256
257 • The excessive flexibility made other extensions essentially
258 impractical. --sparse-index is likely impossible in non-cone mode;
259 even if it is somehow feasible, it would have been far more work to
260 implement and may have been too slow in practice. Some ideas for
261 adding coupling between partial clones and sparse checkouts are
262 only practical with a more restricted set of paths as well.
263
264 For all these reasons, non-cone mode is deprecated. Please switch to
265 using cone mode.
266
268 The "cone mode", which is the default, lets you specify only what
269 directories to include. For any directory specified, all paths below
270 that directory will be included, and any paths immediately under
271 leading directories (including the toplevel directory) will also be
272 included. Thus, if you specified the directory Documentation/technical/
273 then your sparse checkout would contain:
274
275 • all files in the toplevel-directory
276
277 • all files immediately under Documentation/
278
279 • all files at any depth under Documentation/technical/
280
281 Also, in cone mode, even if no directories are specified, then the
282 files in the toplevel directory will be included.
283
284 When changing the sparse-checkout patterns in cone mode, Git will
285 inspect each tracked directory that is not within the sparse-checkout
286 cone to see if it contains any untracked files. If all of those files
287 are ignored due to the .gitignore patterns, then the directory will be
288 deleted. If any of the untracked files within that directory is not
289 ignored, then no deletions will occur within that directory and a
290 warning message will appear. If these files are important, then reset
291 your sparse-checkout definition so they are included, use git add and
292 git commit to store them, then remove any remaining files manually to
293 ensure Git can behave optimally.
294
295 See also the "Internals — Cone Pattern Set" section to learn how the
296 directories are transformed under the hood into a subset of the Full
297 Pattern Set of sparse-checkout.
298
300 The full pattern set allows for arbitrary pattern matches and
301 complicated inclusion/exclusion rules. These can result in O(N*M)
302 pattern matches when updating the index, where N is the number of
303 patterns and M is the number of paths in the index. To combat this
304 performance issue, a more restricted pattern set is allowed when
305 core.sparseCheckoutCone is enabled.
306
307 The sparse-checkout file uses the same syntax as .gitignore files; see
308 gitignore(5) for details. Here, though, the patterns are usually being
309 used to select which files to include rather than which files to
310 exclude. (However, it can get a bit confusing since gitignore-style
311 patterns have negations defined by patterns which begin with a !, so
312 you can also select files to not include.)
313
314 For example, to select everything, and then to remove the file unwanted
315 (so that every file will appear in your working tree except the file
316 named unwanted):
317
318 git sparse-checkout set --no-cone '/*' '!unwanted'
319
320 These patterns are just placed into the $GIT_DIR/info/sparse-checkout
321 as-is, so the contents of that file at this point would be
322
323 /*
324 !unwanted
325
326 See also the "Sparse Checkout" section of git-read-tree(1) to learn
327 more about the gitignore-style patterns used in sparse checkouts.
328
330 In cone mode, only directories are accepted, but they are translated
331 into the same gitignore-style patterns used in the full pattern set. We
332 refer to the particular patterns used in those mode as being of one of
333 two types:
334
335 1. Recursive: All paths inside a directory are included.
336
337 2. Parent: All files immediately inside a directory are included.
338
339 Since cone mode always includes files at the toplevel, when running git
340 sparse-checkout set with no directories specified, the toplevel
341 directory is added as a parent pattern. At this point, the
342 sparse-checkout file contains the following patterns:
343
344 /*
345 !/*/
346
347 This says "include everything immediately under the toplevel directory,
348 but nothing at any level below that."
349
350 When in cone mode, the git sparse-checkout set subcommand takes a list
351 of directories. The command git sparse-checkout set A/B/C sets the
352 directory A/B/C as a recursive pattern, the directories A and A/B are
353 added as parent patterns. The resulting sparse-checkout file is now
354
355 /*
356 !/*/
357 /A/
358 !/A/*/
359 /A/B/
360 !/A/B/*/
361 /A/B/C/
362
363 Here, order matters, so the negative patterns are overridden by the
364 positive patterns that appear lower in the file.
365
366 Unless core.sparseCheckoutCone is explicitly set to false, Git will
367 parse the sparse-checkout file expecting patterns of these types. Git
368 will warn if the patterns do not match. If the patterns do match the
369 expected format, then Git will use faster hash-based algorithms to
370 compute inclusion in the sparse-checkout. If they do not match, git
371 will behave as though core.sparseCheckoutCone was false, regardless of
372 its setting.
373
374 In the cone mode case, despite the fact that full patterns are written
375 to the $GIT_DIR/info/sparse-checkout file, the git sparse-checkout list
376 subcommand will list the directories that define the recursive
377 patterns. For the example sparse-checkout file above, the output is as
378 follows:
379
380 $ git sparse-checkout list
381 A/B/C
382
383 If core.ignoreCase=true, then the pattern-matching algorithm will use a
384 case-insensitive check. This corrects for case mismatched filenames in
385 the git sparse-checkout set command to reflect the expected cone in the
386 working directory.
387
389 If your repository contains one or more submodules, then submodules are
390 populated based on interactions with the git submodule command.
391 Specifically, git submodule init -- <path> will ensure the submodule at
392 <path> is present, while git submodule deinit [-f] -- <path> will
393 remove the files for the submodule at <path> (including any untracked
394 files, uncommitted changes, and unpushed history). Similar to how
395 sparse-checkout removes files from the working tree but still leaves
396 entries in the index, deinitialized submodules are removed from the
397 working directory but still have an entry in the index.
398
399 Since submodules may have unpushed changes or untracked files, removing
400 them could result in data loss. Thus, changing sparse
401 inclusion/exclusion rules will not cause an already checked out
402 submodule to be removed from the working copy. Said another way, just
403 as checkout will not cause submodules to be automatically removed or
404 initialized even when switching between branches that remove or add
405 submodules, using sparse-checkout to reduce or expand the scope of
406 "interesting" files will not cause submodules to be automatically
407 deinitialized or initialized either.
408
409 Further, the above facts mean that there are multiple reasons that
410 "tracked" files might not be present in the working copy: sparsity
411 pattern application from sparse-checkout, and submodule initialization
412 state. Thus, commands like git grep that work on tracked files in the
413 working copy may return results that are limited by either or both of
414 these restrictions.
415
417 git-read-tree(1) gitignore(5)
418
420 Part of the git(1) suite
421
422
423
424Git 2.39.1 2023-01-13 GIT-SPARSE-CHECKOU(1)