🚀 Level up your Git game — subscribers get coupons, early access to blogs/courses, and exclusive YouTube videos.
Subscribe to Learning Ocean · My YouTube Channel · Video: <VIDEO_URL>
Update Git Submodules — Latest Commit, Rollback, Branch Tracking & Sync
In the previous post, we cloned a repository that contains submodules and initialized them correctly.
Now let’s learn how to:
- Update a submodule to the latest commit of its tracked branch
- Commit that pointer change in the parent repo
- Roll back to a previous commit if needed
- Add a submodule that tracks a specific branch
- Sync URLs when a submodule moves (e.g., GitHub → GitLab)
- Handle updates with
--mergeor--rebase
1) Update a submodule to the latest commit
If the submodule repository (e.g., logger) received new commits and you want them:
# Update all submodules to their latest remote-tracked commits
git submodule update --remote
Update only one submodule:
git submodule update --remote logger
If your team prefers a strategy on local changes inside the submodule, add one:
# Merge remote changes into local submodule worktree
git submodule update --remote --merge logger
# Or rebase your local submodule work on top of remote
git submodule update --remote --rebase logger
After the update, your parent repo sees the submodule pointer changed:
git status
# shows: modified: logger (new commits)
git add logger
git commit -m "logger: bump to latest version"
git push origin main
👉 GitHub will now show the submodule at the new commit. Your parent project remains pinned to this exact submodule commit until you change it again.
Visual: pointer moves forward
2) Roll back the submodule to a previous commit
If the latest version introduces breaking changes and you need to revert:
# Enter the submodule
cd logger
git log --oneline # find the older good commit (e.g., abc123)
git checkout abc123 # detach to that commit
cd ..
# Record the pointer change in the parent repo
git add logger
git commit -m "logger: roll back to abc123"
git push origin main
Now the parent repo pins logger at the older (known-good) commit.
3) Track a specific branch when adding a submodule
By default, submodules point to a commit. You can also declare which branch to track for --remote updates:
git submodule add -b submodule git@github.com:user/test-git-hook.git git-test-hook
This writes the branch into .gitmodules:
[submodule "git-test-hook"]
path = git-test-hook
url = git@github.com:user/test-git-hook.git
branch = submodule
Commit the metadata:
git add .gitmodules git-test-hook
git commit -m "add submodule tracking 'submodule' branch"
git push origin main
Later, git submodule update --remote will pull from that declared branch.
4) Keep .gitmodules and .git/config in sync
Sometimes the submodule URL changes (e.g., GitHub → GitLab). You update the URL in .gitmodules, but your local .git/config still points to the old location.
Sync them:
git submodule sync
# or for a single submodule
git submodule sync -- git-test-hook
Verify:
cat .git/config # now shows the new URL under [submodule "..."]
5) Common update flows
With conflicts/local edits inside the submodule?
Use --merge or --rebase to integrate cleanly before committing the new pointer.
6) Quick commands reference
# Update all submodules to their latest tracked commits
git submodule update --remote
# Update only one submodule
git submodule update --remote logger
# Merge or rebase during update
git submodule update --remote --merge logger
git submodule update --remote --rebase logger
# Roll back to an older commit (inside submodule worktree)
cd logger
git checkout <old-commit>
cd ..
git add logger
git commit -m "logger: roll back to <old-commit>"
git push
# Add a submodule tracking a specific branch
git submodule add -b submodule <URL> git-test-hook
# Sync .gitmodules → .git/config after URL change
git submodule sync
Keep Learning 🚀
👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.
👉 My YouTube Channel – More videos, more fun, and lots of learning!
👉 📺 Watch this topic in video form
Stay curious, keep coding, and let’s make learning fun together! 🎉