Why, When and, How to use Git Submodules?

Why use submodules:

  • You use some code review strategy based on Pull/Merge Requests for each repository
  • You have a submodule containing a library that you own
  • You have an application that consumes that library and has it as a submodule

Submodules benefits

  • A team consumes another team module through the git module
  • The business logic/core of a team can be shared across applications
  • Multiple teams can collaborate with a single module — this requires a lot of coordination, testing and etc, let’s leave this for another topic.

Going practical

  • The commit of the magic_solver repository
  • The URL of the magic_solver repository (in the .gitmodules)
  • The location in the tree to put the repository (solvers/magic_solver) — Please be aware that this ‘links’ the repository in .gitmodules to the commit in git index, so don’t change .gitmodules, instead, use git mv or git rm to operate over submodules.
git submodule init
git submodule update
git submodule update --init
git pull --recurse-submodules
git config --global submodule.recurse true
cd solvers/magic_solver
git switch master && git pull
cd ..
git add solvers/magic_solver
git commit -m "Updating magic solver to master"

The WHYs in Git Submodules

The HOWs with CI and Gitmodules

  • Does the CI agent/environment has access to clone the submodule?
  • Does the repository (or the job) will clone it automatically?




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