Git
Submodule
Version Control
Software Development
Git Commands

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:

bash
git submodule status

Update to the latest remote commit

If the submodule tracks a branch:

bash
git submodule update --remote path/to/submodule

This fetches the configured remote branch and checks out its latest commit. Then commit the updated pointer in the parent repository:

bash
git add path/to/submodule
git commit -m "Update submodule to latest commit"

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:

bash
1cd path/to/submodule
2git fetch origin
3git checkout main
4git pull origin main
5cd ../..
6git add path/to/submodule
7git commit -m "Update submodule"

Set the branch --remote follows

If git submodule update --remote does not move where you expect, check .gitmodules:

ini
1[submodule "vendor/lib"]
2    path = vendor/lib
3    url = https://github.com/example/lib.git
4    branch = main

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:

bash
git submodule sync --recursive

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

bash
git submodule update --remote --recursive

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 pull in the parent automatically advances submodules to the latest upstream commit.
  • Committing inside a detached HEAD and later struggling to find those commits.
  • Using --remote without confirming which branch the submodule follows.
  • Updating several submodules at once without checking compatibility or running tests.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions