Pull latest changes for all git submodules
How can you pull the latest changes for all Git submodules in a project?
This question explains how to keep your Git submodules up to date by fetching and updating them using commands like git submodule update and git pull --recurse-submodules.
If your Git project uses submodules, pulling updates isn’t always as simple as running git pull. Submodules are essentially repositories nested inside your main repo, and they need to be updated separately to reflect the latest changes.
Step-by-step: How to update all submodules
First, pull the main repository:
git pull --recurse-submodules
This pulls the latest changes for the main project and the submodule references.
Then, update the submodules to match those references:
git submodule update --recursive --remote
- This command fetches the latest commits from the submodule’s remote branch.
- --recursive ensures that any nested submodules also get updated.
- --remote tells Git to check out the latest commit from the tracked branch (usually main or master).
What’s really happening?
- Without --remote, submodules will just reset to the commit that the parent repo is pointing to.
- With --remote, Git pulls from the submodule’s origin and updates to the latest commit on its remote branch.
Things to watch out for:
- You may need to commit the updated submodule pointers in your main repo.
- Make sure you have permission and access to all submodule remotes.
So, keeping submodules updated is easy once you know the commands—but don’t forget to commit your submodule changes if they move to new commits!