The sequence I usually recommend is, with sparse cone:
git clone --filter=blob:none --no-checkout https://github.com/git/git
cd git
git sparse-checkout init --cone
# that sets git config core.sparseCheckoutCone true
git sparse-checkout set folder1/folder2
git read-tree -mu HEAD
Note that, with With Git 2.34 (Q4 2021), "git add"(man), "git mv"(man), and "git rm"(man) have been adjusted to avoid updating paths outside of the sparse-checkout definition unless the user specifies a --sparse option.
See commit 6579e78, commit 93d2c16, commit d7c4415, commit f9786f9, commit 61d450f, commit 63b60b3, commit 0299a69, commit 49fdd51, commit 105e8b0, commit ed49584, commit f652672, commit edd2cd3, commit ca267ae (24 Sep 2021) by Derrick Stolee (derrickstolee).
(Merged by Junio C Hamano -- gitster -- in commit 2d498a7, 13 Oct 2021)
add: implement the --sparse option
Signed-off-by: Derrick Stolee
We previously modified 'git add'(man) to refuse updating index entries outside of the sparse-checkout cone.
This is justified to prevent users from accidentally getting into a confusing state when Git removes those files from the working tree at some later point.
Unfortunately, this caused some workflows that were previously possible to become impossible, especially around merge conflicts outside of the sparse-checkout cone.
We now re-enable these workflows using a new '--sparse' option to 'git add'.
This allows users to signal "Yes, I do know what I'm doing with these files," and accept the consequences of the files leaving the worktree later.
git add now includes in its man page:
[--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]] [--sparse]
git add now includes in its man page:
--sparse
Allow updating index entries outside of the sparse-checkout cone.
Normally, git add refuses to update index entries whose paths do
not fit within the sparse-checkout cone, since those files might
be removed from the working tree without warning. See
git sparse-checkout for more details.
And:
rm: add --sparse option
Signed-off-by: Derrick Stolee
As we did previously in 'git add'(man), add a '--sparse' option to 'git rm'(man) that allows modifying paths outside of the sparse-checkout definition.
The existing checks in 'git rm' are restricted to tracked files that have the SKIP_WORKTREE bit in the current index.
Future changes will cause 'git rm' to reject removing paths outside of the sparse-checkout definition, even if they are untracked or do not have the SKIP_WORKTREE bit.
git rm now includes in its man page:
--sparse
Allow updating index entries outside of the sparse-checkout cone.
Normally, git rm refuses to update index entries whose paths do
not fit within the sparse-checkout cone. See
git sparse-checkout for more.