Sub topic 5
Updating a Submodule to a Newer Commit
This chapter assumes you are comfortable with everyday Git (commit, branch, merge, push, pull) and that you know what a commit hash identifies. It also assumes you have already added a submodule and cloned a project that contains one. The goal here is to move a submodule from the commit it points at now to a newer one, and to understand what Git records when you do.
The core idea: the parent pins a commit
A submodule is not a branch or a version range. The parent repository stores a gitlink, which is one exact commit hash for the submodule's path. Nothing moves until someone creates a new parent commit that records a different hash.
That gives you two separate questions whenever a submodule seems out of date:
- Which commit does the parent record?
- Which commit is checked out in the submodule's working tree?
Usually these match. They can differ, and most update confusion comes from the difference. The table lists the states you will meet.
| State | Parent records | Submodule checked out | `git status` in parent shows |
|---|---|---|---|
| In sync | commit A | commit A | nothing |
| Moved locally | commit A | commit B | `modified: vendor/libfoo (new commits)` |
| Behind after a pull | commit B | commit A | `modified: vendor/libfoo (new commits)`, but the arrow is reversed in `git diff` |
| Dirty | commit A | commit A plus uncommitted edits | `modified: vendor/libfoo (modified content)` |
Run git diff in the parent to see the hashes. The output looks like this:
diff --git a/vendor/libfoo b/vendor/libfoo
--- a/vendor/libfoo
+++ b/vendor/libfoo
@@ -1 +1 @@
-Subproject commit 3f2a9c1d0e5b7a8c4d6e1f2a3b4c5d6e7f8a9b0c
+Subproject commit 91b7e2c4a6d8f0123456789abcdef0123456789aThe - line is what the parent has recorded. The + line is what is checked out in the submodule.
Two reasons to update
You update a submodule for one of two reasons, and the commands differ.
- You want to follow a teammate. Someone else committed a new submodule pointer to the parent. You pull it, and your submodule checkout must catch up to it.
- You want newer upstream code. The submodule's own repository has new commits, and you decide to adopt them. You move the pointer forward, then commit that move.
Keep these apart. The first is a synchronization step. The second is a deliberate change to the project.
Catching up after you pull the parent
When you run git pull in the parent, Git updates the gitlink in the parent's tree. It does not, by default, change the files inside the submodule. Your submodule stays on the old commit, so git status reports it as modified.
To bring the working tree in line with what the parent records, run:
git pull
git submodule update --init --recursivegit submodule update checks out the commit the parent records. The --init flag registers any submodule that is new since your last pull. The --recursive flag repeats the process for submodules nested inside submodules.
After this, the checked-out commit matches the recorded one and the parent is clean again.
The detached HEAD
git submodule update leaves the submodule on a detached HEAD. Git checks out a specific commit hash, not a branch, so there is no branch to be "on". Inside the submodule, git status says HEAD detached at 91b7e2c.
This is expected and correct for a consumer of the code. It matches the model: the parent pins a commit. It matters later when you want to edit the submodule, which is covered in the next chapter.
Pull and update in one step
You can ask Git to update submodules automatically after a pull:
git pull --recurse-submodulesTo make this the default for commands that support it, set:
git config submodule.recurse trueWith that setting, git pull, git checkout, git switch and similar commands also update submodule working trees. It does not apply to git clone. Treat this as a convenience, and see the configuration chapter for the pitfalls.
Moving to newer upstream code
To adopt new commits from the submodule's own repository, you have two routes. Both end the same way: the parent commits a new gitlink.
Route 1: update from inside the submodule
Enter the submodule and use ordinary Git commands:
cd vendor/libfoo
git fetch
git log --oneline HEAD..origin/main
git checkout 91b7e2c
cd ../..
git add vendor/libfoo
git commit -m "Update libfoo to 91b7e2c"Reviewing git log HEAD..origin/main first lets you see what you are about to adopt. You can check out a specific commit or a tag, which is useful when you want a release and not the branch tip.
Route 2: `git submodule update --remote`
From the parent, one command can fetch and move the submodule:
git submodule update --remote vendor/libfoo
git add vendor/libfoo
git commit -m "Update libfoo to latest upstream"--remote fetches the submodule's remote and checks out the newest commit of a tracked branch. Without --remote, update uses the commit the parent records. With --remote, it ignores that record and uses the remote branch instead. This is the most common source of confusion with the command.
If you omit the path, the command applies to every submodule, which can pull in several unrelated upstream changes at once. Name the submodule you mean.
Which branch is "tracked"?
By default --remote follows the remote's HEAD, which is usually main or master. To follow a different branch, record it in .gitmodules:
git config -f .gitmodules submodule.vendor/libfoo.branch stable
git add .gitmodules
git commit -m "Track the stable branch of libfoo"The resulting entry looks like this:
[submodule "vendor/libfoo"]
path = vendor/libfoo
url = https://github.com/example/libfoo.git
branch = stableYou can also set the branch when adding a submodule with git submodule add -b stable. Note that the branch setting does not make the parent follow that branch automatically. The parent still pins one commit. The setting only tells --remote where to look.
Checking what changed before you commit
A submodule update is a change to your project's dependencies, so review it as you would any other. Useful commands:
# Short summary of the commits being adopted
git diff --submodule=log
# The same information in git status
git status
# A summary for every submodule, including the recorded commit
git submodule summarygit diff --submodule=log replaces the bare hashes with the list of commit subjects between the two pointers. You can make it the default:
git config diff.submodule logHow updates flow
The diagram shows the two update paths and where each one writes.
Common errors
Your update disappears after a pull
You ran git submodule update --remote, saw the new commit, and forgot to commit it in the parent. Later you ran git submodule update, which checked out the old recorded commit again. The move was never saved. Always git add the submodule path and commit after a --remote update.
The parent shows "new commits" you did not make
A teammate pushed a new pointer, you pulled, and you did not run git submodule update. Your checkout is behind the record. Do not run git add on it. That would record the old commit and undo your teammate's update. Run git submodule update --init --recursive instead.
Accidentally reverting a teammate's pointer
This is the same mistake in a more serious form. Running git commit -a while your submodule is out of sync stages the old checkout as a change. Check git status and git diff --submodule=log before committing with -a.
"fatal: remote error: upload-pack: not our ref"
The parent records a commit that the submodule's remote does not have. The person who made the parent commit never pushed the submodule commit. Ask them to push it. When you push yourself, use git push --recurse-submodules=check so Git refuses to push a parent that points to an unpublished commit.
Merge conflicts on a submodule
If two branches moved the same submodule to different commits, Git reports a conflict on the path. Git can resolve it automatically only if one commit is an ancestor of the other. Otherwise, pick the commit you want inside the submodule, then stage the path:
cd vendor/libfoo
git checkout 91b7e2c
cd ../..
git add vendor/libfoo
git commitUpdate strategy options
git submodule update normally checks out the recorded commit. You can change this with --merge or --rebase, which integrate the recorded commit into whatever branch the submodule has checked out. Use them only if you keep local work on a branch inside the submodule. For a pure consumer, the default checkout is the safest choice.
Summary of commands
| Goal | Command |
|---|---|
| Match the checkout to the recorded commit | `git submodule update --init --recursive` |
| Pull the parent and update submodules | `git pull --recurse-submodules` |
| Move to the newest tracked upstream commit | `git submodule update --remote <path>` |
| Preview the commits being adopted | `git diff --submodule=log` |
| Track a specific branch | `git config -f .gitmodules submodule.<name>.branch <branch>` |
| Save the new pointer | `git add <path>` then `git commit` |
Exercise
A teammate says they updated libfoo and pushed. You run git pull, and git status now shows modified: vendor/libfoo (new commits). Your teammate's push already contains the new pointer. What do you run to fix the status, and why should you not run git add vendor/libfoo here?
Show answer
Run git submodule update --init --recursive. The pull already brought the new recorded commit into the parent, but the submodule checkout is still on the old commit, so Git reports it as different. The update command checks out the recorded commit. Running git add vendor/libfoo would stage the old checked-out commit and, once committed, would overwrite your teammate's new pointer with the old one.
Exercise
You run git submodule update --remote vendor/libfoo. The next day, a colleague runs git submodule update after pulling your branch and gets the old libfoo commit. What did you forget, and what two commands fix it?
Show answer
You did not commit the new pointer in the parent repository. --remote only changes the submodule checkout on your machine. Run git add vendor/libfoo and then git commit -m "Update libfoo", then push. After your colleague pulls, git submodule update checks out the new recorded commit.