Thomas S.
Thomas S.

Reputation: 6335

Git submodule prepare for sparse checkout

I have a submodule to be used with sparse checkout. Without sparse checkout I'd do

git submodule update --init <path/to/submodule>

but how to initialize the submodule repository empty without fetching it (or fetching it, but not checking it out) yet, so I can invoke

cd <path/to/submodule>
git config core.sparseCheckout true
cd <../../..>
echo <subdir-to-get> .git/modules/<path/to/submodule>/info/sparse-checkout

Unfortunately

git submodule init <path/to/submodule>

does not create the repository in .git/modules/<path/to/submodule> and the file <path/to/submodule>/.git.

Upvotes: 4

Views: 4861

Answers (2)

fuzzyTew
fuzzyTew

Reputation: 3768

I'm trying using the below approaches, simplified from the other answer by VonC and community.

To init existing submodule using a filter:

git submodule update --init --filter=blob:none

To add submodule using a filter:

git clone --filter=blob:none sub/mod/url [submodpath]
git submodule add sub/mod/url [submodpath] # doesn't download
git submodule absorbgitdirs

Note that the --filter option was added to the git submodule update command in Git 2.36.0

Upvotes: 3

VonC
VonC

Reputation: 1324188

You can try, as in here, to clone the submodule as normal repository first, and then use git submodule absorbgitdirs.

By cloning first with a depth of 1, you don't get too many data:

git clone --depth=1 --no-checkout an/Url <path/to/submodule>
git submodule add an/Url <path/to/submodule>
git submodule absorbgitdirs

Then, you can modify the .git/modules/<path/to/submodule>/info/sparse-checkout

git -C <path/to/submodule> config core.sparseCheckout true
echo 'foo/*' >>.git/modules/<path/to/submodule>/info/sparse-checkout

Finally, get only the files you want:

git submodule update --force --checkout <path/to/submodule>

As commented by Janus, one can do even better (fewer data) with a git clone --filter, which I illustrated in 2019 with "What is the git clone --filter option's syntax?":

#fastest clone possible:
git clone --filter=blob:none --no-checkout https://github.com/git/git
cd git
suburl=$(git config -f .gitmodules --get submodule.<sub>.url)
git submodule update --init --force --checkout <sub>

(Replace <sub> by the submodule name entry from your .gitmodules)

Upvotes: 10

Related Questions