Skip to content

Instantly share code, notes, and snippets.

@ChristopherA
Last active December 13, 2022 14:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristopherA/23ff68d549d990cc7cbbfaacdde4b2ef to your computer and use it in GitHub Desktop.
Save ChristopherA/23ff68d549d990cc7cbbfaacdde4b2ef to your computer and use it in GitHub Desktop.
`git submodule` Tips #git #submodule

git submodule Tips

To clone a repo with submodules

git clone --recursive git@github.com:url_to/awesome_submodule.git path_to_awesome_submodule

Add a submodule clone into an existing repo

git submodule add git@github.com:url_to/awesome_submodule.git path_to_awesome_submodule

If repo with a submodule that is empty

git submodule init

… or to fetch all submodules, even if not initialized yet, and recursively in case submodules have submodules

git submodule update --init --recursive

Grab update inside a single submodule

cd path/to/module
git fetch
git checkout -q <commit-sha1>
cd -
git commit -am “Updated submodule X to: blah blah”

To always see submodule status with git status while a repo

Status, like logs and diffs, is limited to the active repo (right now, the container), not to submodules, which are nested repos. This is often problematic (it’s super easy to miss a regression when limited to this view), so I recommend you set up a submodule-aware status once and for all:

git config status.submoduleSummary true

Useful global .gitcoinfig configurations

git config --global fetch.recurseSubmodules = on-demand # default for git 1.7.5+, but just in case
git config --global diff.submodule log # clearer container diffs when referenced submodule commits changed
git config --global status.submoduleSummary true # git status is useful again when a referenced submodule commit changed

…and aliases

git config --global alias.spull '!git pull && git submodule sync --recursive && git submodule update --init --recursive'
git config --global alias.spush 'push --recurse-submodules=on-demand'

more info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment