Git Submodules Cheat Sheet

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>

References

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thomas Suebwicha

Thomas Suebwicha

3 Followers

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