What Is Sparse Checkout?
Checkout only part of the entire repository. Focus on files you need.
Keep in mind: sparse-checkout settings are only kept in local copy. Won't be pushed. Won't be cloned.
Enable/Set/Add
git sparse-checkout set/add [--no-cone] <file(s)>
set
: re-write current settingsadd
: append to current settings
Running this will automatically enable sparse-checkout feature, which can be checked with git config --list
.
Internally, sparse-checkout maintains two files:
.git/config
, where stores the feature ON/OFF flags.git/info/sparse-checkout
. This file has a format the same as.gitignore
but plays a whitelist role , and is the oneset/add
write to.
Alternatively, the user can edit .git/info/sparse-checkout
manually. (See also: --cone
(default flag) vs --no-cone
)
List Current Settings
git sparse-checkout list
Disable/Re-enable
Disable
git sparse-checkout disable
: turn off all sparse-checkout flags in .git/config
, but not touch .git/info/sparse-checkout
.
Re-enable
git sparse-checkout add # no files here
: turn on flags, continue using current .git/info/sparse-checkout
.
--cone
(default flag) vs --no-cone
--cone
is more like a "smart" mode:
- It automatically add
/*
and!/*/
to.git/info/sparse-checkout
(which means it always allows top-level files such as/README.md
. It is impossible to filter them out.) - When
set/add
, for example, passinglua
will become a/lua/
line in.git/info/sparse-checkout
--no-cone
is somewhat "manual" mode:
- When
set/add
,<file(s)>
will be written into.git/info/sparse-checkout
character by character.
In this mode, the user can filter anything without any restriction. But to remember: always set a path with a leading /
like this: git sparse-checkout add /README.md /lua /doc
.
If you have been aware of all of above, I suggest --no-cone
mode, which is more flexible. An official discussion here .
Extra: Start From a Minimal Clone
git clone --filter=blob:none --no-checkout <URL> [<...>]
git sparse-checkout set [<...>]
git checkout