Sub topic 7
Configuration that helps, and pitfalls to avoid
This chapter assumes you can add, clone, and update a submodule, and that you know a submodule is a gitlink (a pinned commit) plus an entry in .gitmodules. Here you will set a few options that make everyday commands safer, then look at the mistakes that cause most submodule trouble.
Settings that reduce surprises
By default, many Git commands ignore submodules. git pull updates the parent repository but leaves each submodule on its old commit. git push sends the parent without checking whether the submodule commits it references exist on a remote. Three settings address this.
# Show a one-line summary of submodule commits in git status and git diff
git config --global status.submoduleSummary true
git config --global diff.submodule log
# Make pull, checkout, and switch also update submodules
git config --global submodule.recurse true
# Refuse to push a parent whose submodule commits are not yet pushed
git config --global push.recurseSubmodules checkWhat each one does:
| Setting | Effect |
|---|---|
| `status.submoduleSummary` | `git status` lists the commits that differ inside each submodule. |
| `diff.submodule log` | `git diff` shows the submodule's commit messages instead of two bare hashes. |
| `submodule.recurse true` | Commands such as `pull`, `checkout`, and `switch` also update submodules to the commit the parent now records. |
| `push.recurseSubmodules check` | `git push` fails if a referenced submodule commit has not been pushed. |
The check value is the same safeguard you used with --recurse-submodules=check in the previous chapter, now applied on every push.
You can also set a tracked branch for git submodule update --remote. Without this, Git follows the remote's default branch.
git config -f .gitmodules submodule.vendor/libfoo.branch stable
git add .gitmodules
git commit -m "Track the stable branch of libfoo"Because this edits .gitmodules, it is committed and shared with everyone who clones the project.
Common pitfalls
Detached HEAD inside the submodule
A submodule is checked out at a specific commit, not on a branch. If you edit files and commit there without switching to a branch first, those commits belong to no branch. A later git submodule update moves HEAD away and the commits become hard to find. Run git switch -c <branch> (or switch to an existing branch) inside the submodule before you commit, as in the earlier editing example.
Pushing the parent before the submodule
If you commit a new submodule pointer and push only the parent, teammates who pull will fail with an error like fatal: remote error: upload-pack: not our ref. The commit exists only on your machine. Always push the submodule first. The push.recurseSubmodules check setting catches this.
Uncommitted pointer changes
After git pull in a parent without submodule.recurse, git status shows the submodule as modified. The submodule is still on the old commit, and the parent expects a new one. Committing with git commit -a at this point records the old commit and reverts the update. Run git submodule update --init --recursive first.
Empty directories after cloning
A plain git clone creates empty submodule directories. If a build fails because files are missing, check whether the submodules were initialized.
Changing the URL
If a submodule's URL changes, edit .gitmodules, then run git submodule sync to copy the new URL into your local configuration, and git submodule update --init. Until you sync, your clone keeps using the old URL.
Checking your state
These commands show where each submodule stands.
git submodule status
git submodule foreach 'git status --short'In git submodule status, a leading - means the submodule is not initialized, + means its checked-out commit differs from the one the parent records, and U means it has merge conflicts.
Exercise
A teammate pulls your latest parent commit and gets "not our ref" for a submodule. What most likely happened, and which setting would have prevented it?
Show answer
You pushed the parent repository but never pushed the new submodule commit, so the commit the parent points to is not on any remote. Setting push.recurseSubmodules to check makes git push fail until the submodule commit has been pushed.
Exercise
After git pull, git status shows modified: vendor/libfoo (new commits) even though you changed nothing there. What should you run, and why should you avoid git commit -a first?
Show answer
Run git submodule update --init --recursive to move the submodule to the commit the parent records. Committing with -a first would stage the submodule's old commit as a new pointer and undo your teammate's update.