Sub topic 6
Making and publishing changes inside a submodule
This chapter assumes you can add, clone, and update a submodule, and that you know a submodule is a separate repository whose commit the parent records as a gitlink. You should also be comfortable with branches, remotes, and pushing. Here you will fix or extend code that lives in a submodule and publish that change so teammates can get it.
Two repositories, two histories
Inside vendor/libfoo, Git treats the directory as its own repository. The submodule's .git entry is a file that points to .git/modules/vendor/libfoo in the parent. Commands you run there use the submodule's branches, remotes, and history. The parent knows only one fact about the submodule: the commit hash in its gitlink entry.
A change to submodule code therefore involves two commits in two repositories:
- A commit in the submodule that contains the code change.
- A commit in the parent that moves the gitlink to the new submodule commit.
Each commit needs its own push. The order of those pushes is the main thing to get right.
Start on a branch
After git clone --recurse-submodules or git submodule update, the submodule is on a detached HEAD. HEAD points at the exact commit the parent recorded, not at a branch. You can edit files and commit there, but nothing refers to the new commit except HEAD. The next git submodule update checks out the recorded commit again and leaves your work unreachable, except through the reflog.
Create a branch before you change anything:
cd vendor/libfoo
git status
git switch -c fix-parserBefore the switch, git status prints HEAD detached at <short hash>. After it, you are on fix-parser, which starts at the commit the parent pinned.
If you want to work on an existing upstream branch, check it out and make sure it contains the pinned commit:
git fetch origin
git switch main
git merge --ff-only origin/mainSwitching to main may drop you at a commit that is behind or ahead of the one the parent pins. That is fine, as long as you know the parent will pin whatever commit you end up publishing.
Commit and push in the submodule first
Edit, commit, and push inside the submodule as you would in any repository:
cd vendor/libfoo
git switch -c fix-parser
# edit files
git commit -am "Fix parser edge case"
git push origin fix-parserUse git add for new files, because -a only stages changes to tracked files. This push needs write access to the submodule's remote. If you only have read access, fork libfoo and add the fork as a second remote:
git remote add myfork https://github.com/you/libfoo.git
git push myfork fix-parserThen the gitlink will point at a commit that exists on your fork. The parent's .gitmodules URL still names the original repository, so teammates cannot fetch that commit until it is merged upstream or the URL changes. Treat that as a short-lived state.
Record the new commit in the parent
Go back to the parent. git status now shows the submodule as modified:
cd ../..
git statusChanges not staged for commit:
modified: vendor/libfoo (new commits)The parent notices that the submodule's HEAD differs from the committed gitlink. Stage the submodule path, commit, and push:
git add vendor/libfoo
git commit -m "Point libfoo at the parser fix"
git pushgit add vendor/libfoo stages the gitlink with the new hash. Do not write vendor/libfoo/ with a trailing slash. Some Git versions then treat the path as a directory, and the command may fail or stage the wrong thing.
Only now can a teammate pull the parent and run git submodule update to get your fix.
The publish order
If you push the parent first, you publish a gitlink naming a commit that exists only on your machine. A teammate who pulls and runs git submodule update gets an error like this:
fatal: remote error: upload-pack: not our ref 3f2a9c1d...
Fetched in submodule path 'vendor/libfoo', but it did not contain 3f2a9c1d.... Direct fetching of that commit failed.The parent's history is now broken for everyone until you push the submodule commit. Pushing the missing commit fixes it, because the parent's gitlink is already correct.
Let Git check the order
You can ask Git to verify the order when you push the parent:
git push --recurse-submodules=checkWith check, Git looks at each submodule commit the parent's new commits refer to. If any is missing from the submodule's remote, the push is refused:
The following submodule paths contain changes that can not be found on any remote:
vendor/libfoo
Please try
git push --recurse-submodules=on-demand
or cd to the path and use
git push
to push them to a remote.The other useful value is on-demand:
git push --recurse-submodules=on-demandGit first pushes each submodule that has unpushed commits the parent needs, then pushes the parent. If the submodule push fails, the parent push is not attempted.
Both options push the branch the submodule has checked out. A detached HEAD has no branch, so the submodule push will fail. This is another reason to work on a branch.
To make check the default for this repository or for all your repositories:
git config push.recurseSubmodules check
git config --global push.recurseSubmodules check| Value | Behavior |
|---|---|
| `check` | Refuses to push the parent if a referenced submodule commit is not on a remote. Pushes nothing extra. |
| `on-demand` | Pushes submodules with needed unpushed commits first, then the parent. |
| `no` | Ignores submodules. This is the default. |
check is the safer default. It only stops you, and it never pushes anything on your behalf. on-demand is convenient but can push submodule branches you did not intend to publish.
Inspect what Git sees
The default git diff shows a changed submodule as a one-line hash change, which says little. Ask for a summary of the commits:
git diff --submodule=log
git status --short
git submodule summary--submodule=log lists the submodule commits between the old and new gitlink. To make it the default:
git config diff.submodule log
git config status.submoduleSummary trueThe submodule can be dirty in different ways, and git status in the parent reports each differently:
| Parent `git status` says | Meaning | What to do |
|---|---|---|
| `modified: vendor/libfoo (new commits)` | The submodule's HEAD is a different commit than the gitlink. | Stage the path to record it, or run `git submodule update` to discard it. |
| `modified: vendor/libfoo (modified content)` | Tracked files in the submodule have uncommitted edits. | Commit them inside the submodule. |
| `modified: vendor/libfoo (untracked content)` | The submodule has untracked files. | Add or ignore them inside the submodule. |
The parent can never commit uncommitted submodule edits. Staging the path records only the commit hash. If the status says modified content, git add vendor/libfoo stages nothing for those edits, and a commit built from them would lose the work.
Common errors and how to avoid them
| Symptom | Cause | Fix |
|---|---|---|
| Local commits "disappear" after `git submodule update` | You committed on a detached HEAD, and `update` checked out the pinned commit again. | Recover the commit from the reflog, as shown below. |
| Teammates see `not our ref` or `did not contain` | The parent was pushed before the submodule commit. | Push the submodule commit. Prevent it with `push.recurseSubmodules=check`. |
| `git pull` in the parent leaves the submodule on the old commit | `git pull` updates the parent's gitlink but not the submodule working tree. | Run `git submodule update --init --recursive`. |
| `git push` in the submodule says `You are not currently on a branch` | The submodule is on a detached HEAD. | Create a branch with `git switch -c <name>` first. |
| Parent commit contains an unintended submodule change | You used `git commit -a` or `git add .`, which stage the gitlink if the submodule HEAD moved. | Review `git status` before committing, and unstage with `git restore --staged vendor/libfoo`. |
The git commit -a case is worth remembering. A submodule that checked out a different commit during some earlier work will show up in git commit -a as a pointer change, and you can publish it by accident.
Recover commits made on a detached HEAD
If you committed without a branch and have not run git submodule update, create a branch at the current commit:
cd vendor/libfoo
git switch -c rescueIf you already ran update, the commit is no longer at HEAD, but the submodule's reflog still has it:
cd vendor/libfoo
git reflog -5
git switch -c rescue <hash-from-reflog>Look for the entry with your commit message, and branch from its hash. Reflog entries expire (90 days by default for reachable ones, 30 for unreachable ones), so recover soon.
A short checklist
- Run
git statusin the submodule and confirm you are on a branch. - Commit and push in the submodule.
- Run
git statusin the parent and confirm the submodule showsnew commitsand notmodified content. - Stage the submodule path and commit in the parent.
- Push the parent with
--recurse-submodules=check, either directly or throughpush.recurseSubmodules. - Tell teammates to run
git submodule update --init --recursiveafter pulling.
Exercise
You edited vendor/libfoo/parser.c right after a fresh git clone --recurse-submodules, committed with git commit -am "Fix parser" inside vendor/libfoo, and then ran git submodule update in the parent. Your edit seems to be gone. What happened, how do you get it back, and what should you do differently next time?
Show answer
After a clone the submodule is on a detached HEAD, so the commit was not on any branch. git submodule update checked out the commit recorded in the parent again, which moved HEAD away from your commit. The commit still exists in the submodule's reflog. Run git reflog inside vendor/libfoo, find the "Fix parser" entry, and run git switch -c fix-parser <hash>. Next time, run git switch -c <branch> in the submodule before editing, then push the branch, stage the submodule path in the parent, commit, and push the parent with --recurse-submodules=check.
Exercise
A teammate pulls your parent commit and git submodule update fails with not our ref. Which push did you skip, and what setting would have prevented the problem?
Show answer
You pushed the parent without pushing the submodule commit it points to. Push the submodule branch from inside the submodule and the teammate can run git submodule update again. Setting git config push.recurseSubmodules check makes a parent push fail while a referenced submodule commit is missing from its remote.