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