Create a gist now

Instantly share code, notes, and snippets.

.How to add a submodule with shallow checkout and shallow clone
If you have a huge repository (in size and in history) and want to add a subfolder
to your project as a submodule you can follow this example to save time and space
using git's shallow clone and shallow checkout feature. It is a bit more complicated
in this example because I assume that you want your submodule to track a non-default
branch, called `mybranch`, instead of the `master` branch. Things could probably get
a lot simpler when using the default branch. After following the commands in these
examples you can use `git submodule update` and `git submodule update --remote` as normal.
$ git clone --depth 1 --reference ../path/to/mymodcheckout -n --separate-git-dir $gitdir -b mybranch git@github.com:user/repo mymod
$ git submodule add git@github.com:user/repo mymod
$ git config submodule.mymod.branch mybranch
$ git config submodule.mymod.update '!../update.sh'
$ (cd mymod; ../update.sh)
$ git commit -m "add submodule mymod"
$ git submodule init mymod
$ git config submodule.mymod.branch mybranch
$ git config submodule.mymod.update '!../update.sh'
$ gitdir=$(git rev-parse --git-dir)/modules/mymod
$ git clone --depth 1 --reference ../path/to/mymodcheckout -n --separate-git-dir $gitdir -b mybranch git@github.com:user/repo mymod
$ (cd static && ../update.sh)
#!/bin/sh
# alternative to the --checkout option for `git submodule update`
# sets up shallow checkout of the `MyMod` directory in the repo
S=$(git rev-parse --git-dir)/info/sparse-checkout
echo /.gitignore > $S
echo /MyMod >> $S
git config core.sparsecheckout true
git checkout $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment