Git Submodules

Jun 29, 20261 mintechnical

Use submodules when you want to treat two projects as separate yet still be able to use one from within the other.

Example 1: Add an existing Git repo as a submodule of the repo you're working on

git submodule add [repo-url]
  • By default, submodules add the subproject into a directory named the same as the repo -- add a different path for [repo-url] to make it go elsewhere
  • Viewing .gitmodules will show something like: (below is for QR)
[submodule "src/qr_mapping/dependencies/common_robotics_utilities"]
        path = src/qr_mapping/dependencies/common_robotics_utilities
        url = https://github.com/siddancha/common_robotics_utilities.git
[submodule "src/qr_mapping/dependencies/voxelized_geometry_tools"]
        path = src/qr_mapping/dependencies/voxelized_geometry_tools
        url = https://github.com/siddancha/voxelized_geometry_tools.git
[submodule "src/qr_fault/planners/motion-planners"]
        path = src/qr_fault/planners/motion-planners
        url = [email protected]:caelan/motion-planners.git
  • To also initialize, fetch and checkout any nested submodules, you can use the foolproof git submodule update --init --recursive.