Sub topic 5
Updating a submodule to a newer commit
This chapter assumes you can add and clone a submodule, and that you know a submodule is a pointer (a gitlink) to one exact commit in another repository. You should also be comfortable with git fetch, detached HEAD, and committing in the parent repository.
Two meanings of "update"
"Update a submodule" can mean two different things:
- Sync to the recorded commit. A teammate moved the pointer. You want your working copy of the submodule to match it.
- Move to a newer upstream commit. You want the submodule to follow its upstream repository, then record the new pointer.
Git uses the same command, git submodule update, for both. The --remote flag selects the second meaning.
Syncing to the recorded commit
git pull in the parent updates the pointer, but it does not touch the submodule's working tree. Afterward, git status reports the submodule as modified, because its checked-out commit no longer matches the pointer.
git pull
git status
git submodule update --init --recursiveupdate checks out the commit the parent records. The submodule ends up in detached HEAD state, which is normal. --init registers submodules that are new since your clone, and --recursive handles submodules nested inside submodules.
To do this automatically on every pull, set git config submodule.recurse true. The configuration chapter covers this setting.
Moving to a newer upstream commit
To pull in new upstream work, use --remote:
git submodule update --remote vendor/libfoo
git add vendor/libfoo
git commit -m "Update libfoo to latest upstream"--remote fetches from the submodule's remote and checks out the newest commit of the tracked branch. It ignores the commit the parent currently records. The final git add and git commit store the new pointer. Until you commit, the change exists only in your working copy, and nobody else sees it.
Which branch does --remote follow?
By default, Git follows the default branch of the submodule's remote, which is origin/HEAD. To pin a different branch, record it in .gitmodules:
git submodule set-branch --branch stable vendor/libfoo
git submodule update --remote vendor/libfooThis adds a branch = stable line to the .gitmodules entry. Commit .gitmodules so teammates use the same branch. The branch setting only affects --remote. The parent still records an exact commit, so builds stay reproducible.
Pinning to a tag instead
If you want a specific release rather than a moving branch, do it by hand:
cd vendor/libfoo
git fetch --tags
git checkout v2.4.0
cd ../..
git add vendor/libfoo
git commit -m "Pin libfoo to v2.4.0"Preserving local work: checkout, merge, rebase
By default, update runs a checkout, which detaches HEAD. If you have commits on a local branch inside the submodule, the checkout moves away from them. They are not lost, but they are no longer on your current HEAD. Two flags change this behavior:
| Flag | Effect | Use when |
|---|---|---|
| (none) | Checks out the target commit, detached | You make no local commits in the submodule |
| `--merge` | Merges the target commit into your current branch | You want a merge commit |
| `--rebase` | Rebases your current branch onto the target commit | You want a linear history |
Common errors
- Forgetting to commit the pointer. After
--remote, the parent showsmodified: vendor/libfoo (new commits). If you don't commit it, teammates never receive the update. - Assuming
git pullupdated the submodule. It updated only the pointer. Rungit submodule update, or enablesubmodule.recurse. - Committing an older pointer by accident. If you run
git add -Awhile your submodule is behind the recorded commit, you roll the pointer back. Checkgit statusandgit diff --submodulebefore committing. - Uncommitted changes in the submodule.
updatemay refuse to run, or may discard work. Commit or stash first.
Exercise
A teammate pushed a commit that moves the pointer for vendor/libfoo. You run git pull, and git status now shows "modified: vendor/libfoo (new commits)". You have made no changes there. What happened, and which command fixes it?
Show answer
git pull updated the parent's pointer but did not update the submodule's working tree, so the checked-out commit differs from the recorded one. Run git submodule update --init --recursive to check out the recorded commit. No commit is needed, because the parent already records the right pointer.
Exercise
You run git submodule update --remote vendor/libfoo, and the build passes. A colleague later pulls your branch and still gets the old libfoo. What step did you skip?
Show answer
You did not stage and commit the new pointer in the parent repository (git add vendor/libfoo, then git commit), and push it. --remote only changes your local working copy.