Updated answer 2020:
There is now a command git sparse-checkout
, that I present in detail with Git 2.25 (Q1 2020)
nicono's answer illustrates its usage:
git sparse-checkout init --cone # to fetch only root files
git sparse-checkout add apps/my_app
git sparse-checkout add libs/my_lib
It has evolved with Git 2.27 and knows how to "reapply" a sparse checkout, as in here.
Note that with Git 2.28, git status
will mention that you are in a sparse-checked-out repository
Note/Warning: Certain sparse-checkout patterns that are valid in non-cone mode led to segfault in cone mode, which has been corrected with Git 2.35 (Q1 2022).
See commit a3eca58, commit 391c3a1, commit a481d43 (16 Dec 2021) by Derrick Stolee (derrickstolee
).
(Merged by Junio C Hamano -- gitster
-- in commit 09481fe, 10 Jan 2022)
sparse-checkout
: refuse to add to bad patterns
Reviewed-by: Elijah Newren
Signed-off-by: Derrick Stolee
When in cone mode sparse-checkout, it is unclear how 'git sparse-checkout
'(man) add ... should behave if the existing sparse-checkout file does not match the cone mode patterns.
Change the behavior to fail with an error message about the existing patterns.
Also, all cone mode patterns start with a '/
' character, so add that restriction.
This is necessary for our example test 'cone mode: warn on bad pattern', but also requires modifying the example sparse-checkout file we use to test the warnings related to recognizing cone mode patterns.
This error checking would cause a failure further down the test script because of a test that adds non-cone mode patterns without cleaning them up.
Perform that cleanup as part of the test now.
With Git 2.36 (Q2 2022), "git sparse-checkout
"(man) wants to work with per-worktree configuration, but did not work well in a worktree attached to a bare repository.
See commit 3ce1138, commit 5325591, commit 7316dc5, commit fe18733, commit 615a84a, commit 5c11c0d (07 Feb 2022) by Derrick Stolee (derrickstolee
).
(Merged by Junio C Hamano -- gitster
-- in commit 6249ce2, 25 Feb 2022)
worktree
: copy sparse-checkout patterns and config on add
Signed-off-by: Derrick Stolee
Reviewed-by: Elijah Newren
When adding a new worktree, it is reasonable to expect that we want to use the current set of sparse-checkout settings for that new worktree.
This is particularly important for repositories where the worktree would become too large to be useful.
This is even more important when using partial clone as well, since we want to avoid downloading the missing blobs for files that should not be written to the new worktree.
The only way to create such a worktree without this intermediate step of expanding the full worktree is to copy the sparse-checkout patterns and config settings during 'git worktree add
'(man).
Each worktree has its own sparse-checkout patterns, and the default behavior when the sparse-checkout file is missing is to include all paths at HEAD.
Thus, we need to have patterns from somewhere, they might as well be the current worktree's patterns.
These are then modified independently in the future.
In addition to the sparse-checkout file, copy the worktree config file if worktree config is enabled and the file exists.
This will copy over any important settings to ensure the new worktree behaves the same as the current one.
The only exception we must continue to make is that core.bare
and core.worktree
should become unset in the worktree's config file.
Original answer: 2016
git 2.9 (June 2016) will generalize the --no-checkout
option to git worktree add
(the command which allows to works with multiple working trees for one repo)
See commit ef2a0ac (29 Mar 2016) by Ray Zhang (OneRaynyDay
).
Helped-by: Eric Sunshine (sunshineco
), and Junio C Hamano (gitster
).
(Merged by Junio C Hamano -- gitster
-- in commit 0d8683c, 13 Apr 2016)
The git worktree
man page now includes:
--[no-]checkout:
By default, add
checks out <branch>
, however, --no-checkout
can be used to suppress checkout in order to make customizations, such as configuring sparse-checkout.