Sub topic 6
Making and publishing changes inside a submodule
This chapter assumes you can commit, branch, push, and pull in a normal repository. It also assumes you know how to add and clone a submodule. A submodule is a full Git repository nested inside another one. The parent repository (the "superproject") stores only a pointer to one commit of it.
Why order matters
Editing files in a submodule creates commits in two repositories. The submodule has its own history and its own remote. The parent records which submodule commit it wants. Because of this, you must publish in this order:
- Commit and push in the submodule.
- Commit the new pointer in the parent.
- Push the parent.
If you push the parent first, other people get a pointer to a commit that exists only on your machine. Their git submodule update then fails because that commit cannot be fetched.
Step 1: get onto a branch
After git submodule update, a submodule is on a detached HEAD. This means HEAD points at a commit, not a branch. Commits made in this state are not on any branch. They are easy to lose when you next run an update.
Create or switch to a branch before you edit anything.
cd vendor/libfoo
git switch -c fix-parserIf the submodule already has a branch you want to continue, use git switch <branch> instead.
Step 2: commit and push inside the submodule
Work as you would in any repository. Run these commands from inside vendor/libfoo.
# edit files
git commit -am "Fix parser edge case"
git push origin fix-parserYou need push access to the submodule's remote. If you do not have it, fork the project, point the submodule's remote at your fork with git remote set-url origin <fork-url>, and push there.
Step 3: record the new commit in the parent
Go back to the parent. git status now shows the submodule as modified, because its checked-out commit differs from the recorded pointer.
cd ../..
git status
git diff --submodule
git add vendor/libfoo
git commit -m "Point libfoo at the parser fix"git diff --submodule shows the commit messages between the old and new pointer. Plain git diff shows only two hashes.
Run git add vendor/libfoo without a trailing slash. With a slash, some Git versions treat the path as a directory and behave unexpectedly.
Step 4: push with a safety check
git push --recurse-submodules=checkWith check, Git refuses to push the parent if the submodule commit it references has not been pushed. This catches the ordering mistake before anyone else sees it. The option --recurse-submodules=on-demand instead pushes the submodule first, then the parent. It is convenient, but it pushes the submodule's current branch, so confirm you are on the branch you intend.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Commits "disappear" after `git submodule update` | You committed on a detached HEAD, and the update moved HEAD away | Find the commit with `git reflog` inside the submodule, then `git branch rescue <hash>` |
| Teammates see `fatal: remote error: upload-pack: not our ref` | The parent was pushed before the submodule commit | Push the submodule branch, then ask them to retry |
| `git status` shows `modified: vendor/libfoo (modified content)` | Uncommitted edits inside the submodule | Commit or discard them in the submodule |
| `git status` shows `(new commits)` | The submodule HEAD is ahead of the recorded pointer | Stage and commit the pointer, or reset the submodule |
Exercise
You committed a fix in vendor/libfoo and ran git commit -am in the parent, then pushed the parent. A teammate reports that git submodule update fails. What did you skip, and how do you repair it?
Show answer
You skipped pushing the submodule commit to the submodule's remote, so the parent points at a commit nobody else can fetch. Repair it by running git push origin <branch> inside vendor/libfoo. To prevent it, push with git push --recurse-submodules=check.
Exercise
After running git submodule update, you edit a file in the submodule and commit. Later another git submodule update makes your commit vanish from the working tree. Why, and how do you get it back?
Show answer
The submodule was on a detached HEAD, so the commit belonged to no branch. The update moved HEAD to the recorded pointer. Run git reflog in the submodule, find the commit, and run git branch rescue <hash> to attach it to a branch.