Git Submodules Cheat Sheet

Thomas Suebwicha
2 min readAug 21, 2021

By Thomas Suebwicha
Written March 31, 2021
Last Edited August 21, 2021

Features

  • Separate dependence of Git commits of that repository
  • Independent library can be referenced to specific commit/branch development
# Clone a new project and its submodules  
git clone --recursive <MAIN_PROJECT_REPO>
# Create a submodule (& clone it)
# This also creates a .gitmodules file that shouldn't be editted manually
git submodule add <MODULE_REPO> <DIRECTORY>
# Checkout a branch on the submodule
cd <MODULE_REPO>
git checkout <BRANCH_NAME>
# Commit the version
cd ..
git commit -am "<COMMENT>"
git push origin <BRANCH>
# update the submodules
git submodule update
# Git cloning submodules if empty
git submodule update --init --recursive
# Update specific submodule
git submodule update --remote <MODULE_DIRECTORY>
# PULL on all submodules
git submodule foreach git pull origin master
# Update submodules recursively
git submodule update --recursive
# Status of git submodule
git submodule status <PATH_TO_SUBMODULE>
# Modifications to git project
## Want submodule to update after a pull - add to githook
echo "git submodule update --init --recursive" >> .git/hooks/post-merge
## Set up git to show updates to submodules on git status
git config status.submodulesummary 1
## Point the submodule to a branch instead of a commit E.g. master or stable
git config -f .gitmodules submodule.<MODULE_NAME>.branch <BRANCH_NAME>

Another alternative is to identify if the dependency has a release tag and download the version. This will mitigate the dependence and simplify the process and dependence of the above commands.

References

--

--

Thomas Suebwicha

An entry developer wanting to share my knowledge and bring up others. Also an active person just trying to learn from other people mistakes.