Sub topic 7
Configuration that helps, and pitfalls to avoid
This chapter assumes you can add, clone, update, and edit a submodule, as covered in the earlier chapters. You should know what a gitlink is and that .gitmodules maps paths to URLs. Submodule commands work, but their defaults surprise people. A few configuration settings remove most of the surprises. The rest you avoid by knowing how each failure looks.
Why the defaults cause trouble
Most Git commands treat a submodule as a single entry in the parent's tree: a path and a commit hash. They don't touch the submodule's files unless you ask them to. This leads to a common gap:
git pull,git checkout, andgit switchmove the parent to a different commit. The recorded submodule commit may change, but the checked-out files in the submodule stay where they were.git statusthen reports the submodule as modified, even though you changed nothing.git pushsends the parent's commits and ignores the submodule's commits. This is how a parent ends up pointing at a commit that exists only on your machine.
Each setting below closes part of this gap.
Settings that make routine commands safer
Recurse into submodules by default
submodule.recurse makes commands that accept --recurse-submodules use it automatically. These commands include checkout, switch, pull, fetch, reset, restore, grep, and read-tree. It does not affect git clone, which still needs its own flag.
git config --global submodule.recurse trueAfter this, git pull and git switch also update the submodule working trees to match the parent's recorded commits. You no longer end up with a "modified" submodule that is only stale.
Check submodule pushes
push.recurseSubmodules controls what git push does about submodule commits.
# Refuse to push the parent if a referenced submodule commit is not on a remote
git config --global push.recurseSubmodules check
# Or push the submodule commits automatically first
git config --global push.recurseSubmodules on-demandcheck is the more conservative choice. It stops the push and tells you which submodule needs pushing. on-demand pushes the submodule for you. It uses the submodule's own remote and your credentials for it, so it fails if you lack write access there.
Make status and diff readable
By default, git diff shows a submodule change as two raw hashes. Two display settings turn that into something you can review:
git config --global status.submoduleSummary true
git config --global diff.submodule logWith them, git status includes a short summary of the commits in a moved submodule. git diff lists the commit subjects between the old and new pointer.
Fetch what the parent needs
fetch.recurseSubmodules accepts yes, no, or on-demand. The default is on-demand: when the parent's fetched commits point at submodule commits you don't have, Git fetches them. Leave it alone unless a script has disabled it. Teams that set it to no for speed run into "reference is not a tree" errors later.
Settings stored in .gitmodules
Some options describe the project, not your machine. Put those in .gitmodules so every clone gets them. A typical entry has a tracked branch and a relative URL:
[submodule "vendor/libfoo"]
path = vendor/libfoo
url = ../libfoo.git
branch = main
ignore = dirtybranchtellsgit submodule update --remotewhich remote branch to follow. Without it, Git follows the remote'sHEAD. Set it withgit submodule set-branch --branch main vendor/libfoo(Git 2.22 and later).url = ../libfoo.gitis a relative URL. Git resolves it against the parent's remote URL, so a project cloned over SSH fetches its submodule over SSH, and one cloned over HTTPS uses HTTPS. This helps with mirrors and forks. It breaks if the parent and submodule don't live side by side on the same host.ignore = dirtyhides uncommitted changes inside the submodule from the parent'sgit statusandgit diff. The pointer itself, meaning a different checked-out commit, still shows. The other values areuntracked,all, andnone(the default). Useallonly if you accept that pointer moves also disappear from status. That can hide real changes.updatechooses howgit submodule updateplaces the commit:checkout(the default),rebase,merge, ornone. The valuenoneskips the submodule entirely, which is useful for large optional dependencies.
After you edit .gitmodules by hand, run git submodule sync. Git copies the URL into .git/config when you first initialize a submodule, so a later change in .gitmodules has no effect until you sync. To change a URL, git submodule set-url vendor/libfoo <url> (Git 2.25 and later) edits the file, and git submodule sync applies it.
The settings fall into two groups:
| Setting | Where it lives | Who it affects |
|---|---|---|
| `submodule.recurse` | your global or local Git config | only you |
| `push.recurseSubmodules` | your global or local Git config | only you |
| `diff.submodule`, `status.submoduleSummary` | your global or local Git config | only you |
| `branch`, `url`, `ignore`, `update` | `.gitmodules`, committed | everyone who clones |
Pitfall: the detached HEAD
git submodule update checks out the recorded commit directly, not a branch. Inside the submodule, git status says HEAD detached at 3f2a9c1. If you commit there without creating a branch, the new commit is reachable only from HEAD. The next git submodule update or git switch in the parent moves HEAD away and the commit is no longer on any branch.
Create a branch before you edit, as shown in the earlier chapter on changing code inside a submodule. If you already committed on a detached HEAD, recover the commit like this:
cd vendor/libfoo
git reflog -5
git branch rescue-parser 8c1d4e7
git switch rescue-parserUse the hash from the reflog output. The reflog keeps unreachable commits for at least 30 days by default, so rescue them soon.
Pitfall: the parent points at a commit nobody else has
This is the most common failure. You commit in the submodule, then commit the parent with the new pointer, then push only the parent. A teammate pulls and gets an error like this:
fatal: remote error: upload-pack: not our ref 8c1d4e7...
Errors during submodule fetch:
vendor/libfooThe parent's history is fine, but the commit it names exists only on your disk. The fix is to push the submodule commit from your machine. Prevention is push.recurseSubmodules=check, because it makes the parent push fail until the submodule commit is published. Always push in the order submodule first, parent second.
Pitfall: stale submodules after switching branches
Suppose branch main pins libfoo at commit A and branch feature pins it at commit B. If you run git switch feature without recursion, the submodule stays at A. git status reports modified: vendor/libfoo (new commits). If you then run git commit -a, you record A as the pointer on the feature branch and quietly undo the intended change.
The remedy is to update the submodules after every switch, or to set submodule.recurse true so Git does it for you. Before any git commit -a, read git status. Prefer git add <path> for the files you mean to commit.
git switch feature
git submodule update --init --recursive
git submodule statusReading git submodule status
git submodule status prints one line per submodule. The first character tells you its state:
| Prefix | Meaning | Usual fix |
|---|---|---|
| (space) | Checked-out commit matches the parent's record | none needed |
| `-` | Not initialized | `git submodule update --init` |
| `+` | Checked-out commit differs from the record | `git submodule update`, or commit the new pointer if the change is intended |
| `U` | Merge conflict in the submodule pointer | pick a commit, then `git add` the path |
A triage flow for a "modified" submodule
When git status shows a submodule as modified, decide which of three situations you are in before you act.
Pitfall: local paths and the file protocol
Newer Git versions (2.38.1 and later) refuse to clone a submodule from a local filesystem path by default. This is a security fix. It matters when you practice with two repositories on one disk. You will see fatal: transport 'file' not allowed. For a throwaway experiment, allow it for that single command:
git -c protocol.file.allow=always submodule add ../libfoo.git vendor/libfooDon't set protocol.file.allow=always globally. A malicious repository could use it to read files from your machine.
Pitfall: URL changes and forks
If libfoo moves to a new host, update .gitmodules and commit it. Every collaborator must then run git submodule sync --recursive to refresh .git/config. Without it, their clones keep fetching from the old URL, which may still work until the old repository is deleted.
If you work from a fork, the parent's .gitmodules may point at the upstream project while you want your fork. Override the URL locally and leave .gitmodules untouched:
git config submodule.vendor/libfoo.url https://github.com/you/libfoo.gitThat setting lives in .git/config, so it stays on your machine and does not reach collaborators.
Pitfall: tools that skip submodules
Several things ignore submodules unless told otherwise:
- A CI job that runs
git cloneor a default checkout step builds with empty directories. Enable the CI system's submodule option, or rungit submodule update --init --recursiveas a build step. - Archive downloads from hosting sites often leave out submodule content. A tarball of the parent has an empty
vendor/libfoo. git grepsearches only the parent unless you pass--recurse-submodules. Thesubmodule.recursesetting covers this.git submodule foreachruns a command in each checked-out submodule. For example,git submodule foreach 'git status --short'gives a quick overview. Add--recursivefor nested submodules.
A starting configuration
This block combines the safer defaults from the chapter. Run it once per machine:
git config --global submodule.recurse true
git config --global push.recurseSubmodules check
git config --global status.submoduleSummary true
git config --global diff.submodule logSummary of habits
- Enable
submodule.recurseandpush.recurseSubmodules=checkon every machine. - Create a branch inside a submodule before editing.
- Push the submodule first, then the parent.
- After switching branches or pulling, confirm that
git submodule statushas no+or-lines you did not expect. - Run
git submodule syncafter any URL change.
Exercise
A teammate pulls your latest parent commit and sees "not our ref" while Git fetches vendor/libfoo. Your own checkout works fine. What most likely happened, how do you fix it, and which setting would have prevented it?
Show answer
You committed in the submodule and then pushed only the parent, so the parent points at a submodule commit that exists only in your local clone. Fix it by running git push from inside vendor/libfoo (pushing the branch that contains that commit), after which your teammate can fetch it. Setting push.recurseSubmodules to check makes the parent push fail until the submodule commit is published. Setting it to on-demand pushes the submodule commit automatically.
Exercise
You run git switch feature and then git status shows modified: vendor/libfoo (new commits), although you edited nothing. What is the cause, what command resolves it, and which config setting avoids it?
Show answer
The feature branch pins a different submodule commit than main, but git switch did not move the submodule's working tree, so its checked-out commit no longer matches the parent's record. Run git submodule update --init --recursive to check out the recorded commit. Setting submodule.recurse to true makes git switch update submodules automatically.