How to Update Submodule in Git: Simple Commands and Examples
To update a Git submodule, use
git submodule update --remote to fetch and checkout the latest commit from the submodule's remote branch. Then commit the change in the main repository to record the updated submodule state.Syntax
The basic command to update a Git submodule is:
git submodule update --remote [--merge|--rebase]: Updates the submodule to the latest commit from its configured remote branch.git submodule update: Checks out the commit recorded in the main repository (no update from remote).git add [submodule_path]: Stage the updated submodule pointer.git commit -m "Update submodule": Commit the updated submodule reference.
The --remote flag tells Git to fetch the latest changes from the submodule's remote repository instead of just using the recorded commit.
bash
git submodule update --remote
# Then stage and commit the change
git add path/to/submodule
git commit -m "Update submodule to latest commit"Example
This example shows how to update a submodule named libs/mylib to its latest commit on the remote branch and commit the change in the main repository.
bash
git submodule update --remote libs/mylib cd libs/mylib # Optional: check current commit git log -1 --oneline cd ../.. git add libs/mylib git commit -m "Update mylib submodule to latest commit"
Output
Updating libs/mylib
commit abc1234 Update README
[main 9f8e7d6] Update mylib submodule to latest commit
Common Pitfalls
Common mistakes when updating submodules include:
- Running
git submodule updatewithout--remoteonly resets the submodule to the commit recorded in the main repo, not the latest remote commit. - Forgetting to
git addand commit the submodule path after updating, so the main repo does not track the new submodule commit. - Not initializing submodules first with
git submodule initor cloning with--recurse-submodules.
Wrong way:
git submodule update libs/mylib # This does not fetch latest remote changes
Right way:
git submodule update --remote libs/mylib git add libs/mylib git commit -m "Update submodule"
Quick Reference
| Command | Description |
|---|---|
| git submodule update --remote | Fetch and checkout latest commit from submodule remote branch |
| git add [submodule_path] | Stage updated submodule pointer for commit |
| git commit -m "message" | Commit the updated submodule reference in main repo |
| git submodule init | Initialize submodules after cloning |
| git clone --recurse-submodules [repo] | Clone repo with all submodules initialized |
Key Takeaways
Use 'git submodule update --remote' to fetch the latest submodule commits from its remote branch.
Always stage and commit the submodule path after updating to record the change in the main repository.
Without '--remote', 'git submodule update' resets submodules to the commit recorded in the main repo, not the latest remote commit.
Initialize submodules with 'git submodule init' or clone with '--recurse-submodules' to avoid missing submodule data.
Check the submodule's current commit with 'git log -1' inside the submodule directory after updating.