How can I pull the latest changes for all Git submodules?

We’re using Git submodules to manage dependencies across several large internal libraries. Each library is maintained in its own repo and added to the main project as a submodule.

During active development, we often want to pull the latest version of every submodule to stay in sync. However, running git pull in the root project directory doesn’t seem to update the submodules automatically.

What’s the correct way to git pull submodules so that each one fetches and checks out its latest commit from the remote repo? Is there a single command that can handle this, or does it need to be scripted manually?

Hey! When you run git pull in your main repo, it doesn’t automatically update the submodules because they’re separate repositories tracked by specific commits.

The best way I’ve found to git pull submodules is to run this command after pulling the main repo:

git submodule update --remote --merge

This command goes into each submodule, fetches the latest changes from the remote tracking branch (usually master or main), and merges them into your current submodule commit. So the workflow looks like:

git pull origin main       # Pull latest main repo changes
git submodule update --remote --merge  # Update all submodules to their latest remote commits

This keeps your submodules in sync without manually going into each directory.

From my experience, the easiest way to git pull submodules in one go is using the --remote flag with git submodule update. You can do:

git submodule update --remote

This fetches and checks out the latest commits in the remote branches of all submodules. Just keep in mind, this doesn’t automatically commit the updated submodule pointers in your main repo.

You’ll want to commit those changes yourself if you want the main repo to track the new submodule commits.

If you want to pull and update submodules in one shot, you can chain commands in a script:

git pull && git submodule update --remote

This handles syncing everything smoothly.

I usually script it to be safe and explicit. Since git pull alone won’t update submodules, you can use this snippet to pull the main repo and then iterate over submodules to pull their latest changes:

git pull origin main
git submodule foreach git pull origin main

This runs git pull origin main inside every submodule folder. You can customize the branch name if your submodules use something else.

This approach gives you full control and works great if you want to see each submodule’s update output separately.