Git update submodule to latest commit
Codemia Knowledge Hub ·
Run git submodule update --remote path/to/submodule to move a submodule to the latest commit on its tracked branch, then commit the changed submodule path in the parent repository. A submodule is a reference to one specific commit in another repository, so "updating" means moving that pointer and recording the move.
Why a submodule does not follow its branch
When you clone a repository with a submodule, Git checks out the submodule in a detached HEAD state at the exact commit recorded by the parent. That is why it does not move when new commits land upstream.
To see the commit currently pinned by the parent repository:
Update to the latest remote commit
If the submodule tracks a branch:
This fetches the configured remote branch and checks out its latest commit. Then commit the updated pointer in the parent repository:
Without this commit, other developers will not get the new submodule revision when they pull.
Update manually from inside the submodule
You can also treat the submodule as a normal repository. This is the way to land on a specific commit or branch, or to inspect changes and resolve conflicts before recording the new commit:
Set the branch --remote follows
If git submodule update --remote does not move where you expect, check .gitmodules:
The branch entry tells Git which remote branch to follow. Without it, the behavior may not match your intent. After editing .gitmodules, sync the configuration:
Detached HEAD is normal
A detached HEAD inside a submodule is the default state for a checkout at a specific commit, and is fine for read-only use. It only becomes a problem if you make changes inside the submodule without switching to a branch first.
Update all submodules at once
This updates every configured submodule recursively. Review what changed before committing in the parent repository.
Mistakes to avoid
- Updating the submodule locally and forgetting to commit the new pointer in the parent repo.
- Assuming
git pullin the parent automatically advances submodules to the latest upstream commit. - Committing inside a detached
HEADand later struggling to find those commits. - Using
--remotewithout confirming which branch the submodule follows. - Updating several submodules at once without checking compatibility or running tests.
Related reading
- Update Git branches from master
- Update git commit author date when amending
- Update Git submodule to latest commit on origin
- Updates were rejected because the tag already exists when attempting to push in SourceTree
- Updates were rejected because the tip of your current branch is behind its remote counterpart
- Updating a local repository with changes from a GitHub repository
- Updating and committing only a file's permissions using git version control
- Updating version numbers of modules in a multi-module Maven project
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.