Why Git Submodules?
Every git project is stored as a versioned code repository (a repo). Git Submodules allow us to reference other repos within a project, which effectively puts a project inside a project (or a repo inside a repo). The submodule’s code can then be used by the main project, but the submodule maintains its own commit and branch history, which allow the projects to be checkout to independent branches.
Possible Benefits
- separate rollbacks - Including a Library as a submodule, updating to a new feature commit suddenly breaks your whole project? Rollback in production, before searching the problem
- branch switching - Its possible to develop and test a new feature in the main project and test it with multiple commits or branches of the submodule and vice-versa
Caveats
Juggling multiple repos - With more Submodules comes more administration.
Possible Solutions
Recursive updating the Submodules:
git submodule update --init --recursive
git submodule foreach --recursive git fetch
Conflicts?
Fast forward might help:
git submodule foreach git pull --ff-only origin <branchname>
Otherwise a merge is needed:
git submodule foreach git merge origin <branchname>