0
0
Gitdevops~10 mins

Why submodules manage nested repos in Git - Visual Breakdown

Choose your learning style9 modes available
Process Flow - Why submodules manage nested repos
Main Repo
Add Submodule
Submodule Repo Cloned
Main Repo Tracks Submodule Commit
Update Submodule Separately
Commit Submodule Change in Main Repo
Shows how a main git repo adds and tracks a nested repo as a submodule, managing its commit separately.
Execution Sample
Git
git submodule add https://github.com/example/lib.git libs/lib
cd libs/lib
git checkout v1.0
cd ../..
git commit -am "Add lib submodule at v1.0"
Adds a nested repo as a submodule, checks out a version, then commits the submodule reference in the main repo.
Process Table
StepActionResultMain Repo StateSubmodule State
1Run 'git submodule add https://github.com/example/lib.git libs/lib'Submodule cloned into libs/libTracks submodule path and commitCloned at HEAD of default branch
2cd libs/libEnter submodule directoryNo changeInside submodule repo
3git checkout v1.0Submodule checked out at tag v1.0No changeDetached HEAD at v1.0
4cd ../..Return to main repo rootNo changeNo change
5git commit -am "Add lib submodule at v1.0"Main repo records submodule commitSubmodule commit recorded in .gitmodules and indexNo change
6Later, update submoduleSubmodule can be updated independentlyMain repo must commit updated submodule commitSubmodule can move to new commit
7ExitProcess completeMain repo tracks specific submodule commitSubmodule is nested repo managed separately
💡 Submodule manages nested repo commit separately; main repo tracks submodule commit reference.
Status Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
Main Repo .gitmodulesemptycontains submodule path and URLno changetracked submodule committracked submodule commit
Submodule Commitnonecloned at HEADchecked out v1.0 tagno changecan update independently
Key Moments - 3 Insights
Why does the main repo track a specific commit of the submodule?
Because the main repo records the exact commit of the submodule it uses, ensuring consistent versions (see execution_table step 5).
Can you update the submodule independently from the main repo?
Yes, you can update the submodule repo separately, but you must commit the new submodule commit in the main repo to track the change (see execution_table step 6).
What happens if you don't commit the submodule update in the main repo?
The main repo will still point to the old submodule commit, causing version mismatch (see execution_table step 6 and exit_note).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the submodule state?
ACloned at HEAD of default branch
BDetached HEAD at tag v1.0
CSubmodule commit recorded in main repo
DNo submodule present
💡 Hint
Check the 'Submodule State' column at step 3 in execution_table.
At which step does the main repo record the submodule commit?
AStep 1
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the 'Main Repo State' column for when the commit happens in execution_table.
If you update the submodule but do not commit in the main repo, what happens?
AMain repo tracks old submodule commit causing mismatch
BMain repo tracks new submodule commit automatically
CSubmodule is deleted
DMain repo breaks and cannot commit
💡 Hint
Refer to key_moments about committing submodule updates and execution_table step 6.
Concept Snapshot
Git submodules let you include a nested repo inside a main repo.
You add a submodule with 'git submodule add <url> <path>'.
The main repo tracks a specific commit of the submodule.
You update submodules separately and commit their new commit in main repo.
This keeps nested repos versions consistent and manageable.
Full Transcript
This visual execution shows how git submodules manage nested repositories. First, you add a submodule to your main repo, which clones the nested repo inside a folder. Then you can checkout a specific commit or tag in the submodule. The main repo tracks this exact commit in its .gitmodules and index. Later, you can update the submodule independently, but you must commit the new submodule commit in the main repo to keep versions consistent. If you don't commit the update, the main repo still points to the old submodule commit, causing mismatch. This process helps manage nested repos clearly and safely.