Skip to content

Instantly share code, notes, and snippets.

@Gikkman
Last active March 19, 2025 09:18
Show Gist options
  • Save Gikkman/819613d7be1d3a8f89c0bcbc0107b956 to your computer and use it in GitHub Desktop.
Save Gikkman/819613d7be1d3a8f89c0bcbc0107b956 to your computer and use it in GitHub Desktop.
Git spare-checkout with nested submodules
We got a root project (project-a) which has a submodule (project-b). That submodule in turn has a submodule (project-c).
Each project also contains some files.
project-a @ 5r210h6/
├─ some-file.a
├─ other-file.a
├─ third-file.a
├─ project-b @ 196a095/
│ ├─ some-file.b
| ├─ more-file.b
│ ├─ project-c @ 8050772/
│ │ ├─ file.c
│ ├─ chonky-folder-b/
│ │ ├─ huge-file.b
We got the entire project-a checked out, but we wish to checkout project-b and project-c,
but not chonky-folder-b and it's content.
The key trick here is to configure sparse-checkout in the submodule, then use 'git submodule absorbgitdirs' to hoist the
git config from the submodule into the parent module, then run 'git submodule update'. This will then apply the
sparse-checkout config when running the submodule update.
// This approach says: Sparse checkout everything in the root folder of 'project-b' and downwards, but skip 'chonky-folder-b'
cd project-a
git submodule init project-b
git clone --no-checkout --depth=1 $(git config submodule.project-b.url) project-b
cd project-b
git sparse-checkout set --cone '/*' '!/chonky-folder-b/'
cd -
git submodule absorbgitdirs project-b
git submodule update --init --recursive --force project-b
// This approach says: Sparse checkout everything in the root folder of 'project-b', but nothing in a subfolder of project-b,
// except for 'project-c'
cd project-a
git submodule init project-b
git clone --no-checkout --depth=1 $(git config submodule.project-b.url) project-b
cd project-b
git sparse-checkout set --cone '/*' '!/*/' '/project-c/'
cd -
git submodule absorbgitdirs project-b
git submodule update --init --recursive --force project-b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment