Git commit to common submodule master branch
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Committing changes in a Git submodule is a two-repository workflow, not a single commit operation. You make a normal commit inside the submodule repository first, then you commit the updated submodule pointer in the parent repository. Most confusion comes from forgetting that the parent repo does not store the submodule’s files directly; it stores a reference to a specific submodule commit.
Understand What The Parent Repo Tracks
A submodule in the parent repository is tracked as a commit pointer, not as a copied working tree snapshot. That means changing files inside the submodule is not enough. The parent repository only notices that the submodule now points at a different commit.
So the full workflow is:
- commit inside the submodule,
- push the submodule branch if needed,
- go back to the parent repo,
- commit the updated submodule reference there.
Step 1: Enter The Submodule And Check The Branch
A common issue is that submodules are often checked out in detached HEAD state.
If you want to commit on the submodule’s master branch, check it out explicitly.
If master should track the remote branch, make sure it is set up correctly before you start editing.
Step 2: Commit Inside The Submodule
Now treat the submodule like a normal Git repository.
At this point, the new submodule commit exists in the submodule repository. The parent repository still points to the old commit until you update and commit that pointer.
Step 3: Commit The Updated Pointer In The Parent Repo
Return to the parent repository and inspect its status.
You will typically see the submodule path reported as modified. That means the parent repo noticed the referenced submodule commit changed.
Commit that change like any other tracked update.
Now collaborators pulling the parent repository will receive the updated submodule reference.
What Happens If You Skip The Parent Commit
If you only commit inside the submodule and forget the parent repository commit, your local checkout points at the new submodule revision, but the parent repository history does not record that dependency update. Other developers will not automatically land on the same submodule commit.
That is one of the most common submodule mistakes.
Detached HEAD Is The Common Trap
A lot of “why did my submodule commit disappear?” stories come from committing while the submodule is detached instead of on a normal branch.
Check before committing:
If the result is HEAD, you are detached. Switch to the intended branch first unless you know exactly why detached work is acceptable.
Keep Branch Strategy Explicit
If several parent repositories share the same submodule, agreeing on branch policy matters.
For example:
- submodule development happens on its own
masterormain, - parent repos pin specific tested commits,
- updating the parent pointer is a deliberate dependency upgrade.
This is safer than assuming the submodule should magically follow the latest branch tip at all times.
Common Pitfalls
- Committing inside the submodule but forgetting to commit the updated pointer in the parent repo.
- Working in a detached submodule
HEADand then being surprised by later branch confusion. - Assuming the parent repo stores submodule file diffs directly instead of a commit reference.
- Pushing the parent repo update before the referenced submodule commit has been pushed to its remote.
- Treating submodules like ordinary directories rather than nested repositories with separate history.
Summary
- A submodule commit is a normal commit inside the submodule repository.
- The parent repository must also commit the updated submodule pointer.
- Check that the submodule is on the intended branch before committing.
- Push the submodule commit before publishing the parent repo update.
- Think of submodules as separate repositories whose versions are pinned by the parent project.
Related reading
- Git conflict markers
- Git Confusion about merge algorithm, conflict format, and interplay with mergetools
- Git Corrupt loose object
- Git Could not resolve host github.com error while cloning remote repository in git
- Git, Create a branch from unstaged/uncommitted changes on master
- Git Create a branch from unstaged/uncommitted changes on master
- git 'credential-cache' is not a git command
- Git diff -w ignore whitespace only at start end of lines
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.