Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save kouk/3ba77edce12e95c1f779 to your computer and use it in GitHub Desktop.
Save kouk/3ba77edce12e95c1f779 to your computer and use it in GitHub Desktop.
.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