0
0
Gitdevops~10 mins

Adding a submodule in Git - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Adding a submodule
Start in main repo
Run git submodule add <url> <path>
Git clones submodule repo into <path>
Git records submodule info in .gitmodules
Stage .gitmodules and submodule path
Commit changes
Submodule added successfully
This flow shows how adding a submodule clones another repo inside your repo, records it, stages, and commits the change.
Execution Sample
Git
git submodule add https://github.com/example/lib.git libs/lib
git commit -m "Add lib submodule"
Adds a submodule from a remote repo into the 'libs/lib' folder and commits the change.
Process Table
StepCommandActionResult
1git submodule add https://github.com/example/lib.git libs/libClone submodule repo into libs/lib, update .gitmodules, and automatically stage changesSubmodule repo cloned; .gitmodules updated with submodule info; changes staged
2git statusCheck staged changes.gitmodules and libs/lib show as staged new changes
3git commit -m "Add lib submodule"Commit staged changesCommit created with submodule addition
4git statusVerify clean working directoryNo changes, working tree clean
5git submodule statusVerify submodule statusShows libs/lib (commit hash); submodule tracked correctly
💡 Submodule added and committed; working directory clean
Status Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
.gitmodulesdoes not existcreated with submodule info, stagedcommittedcommittedpresent in repo
libs/lib folderdoes not existcloned submodule repo, stagedcommittedcommittedtracked as submodule
Key Moments - 3 Insights
Why do we need to commit after adding a submodule?
Because adding a submodule changes the .gitmodules file and adds a folder, these changes must be committed to record the submodule in the main repo (see execution_table step 3).
What does the .gitmodules file do?
It stores the URL and path info of the submodule so Git knows where to fetch it from later (see variable_tracker for .gitmodules creation at step 1).
Is the submodule content stored inside the main repo?
No, the submodule is a separate Git repo cloned inside a folder; the main repo tracks only the commit reference and path (see execution_table step 1 and variable_tracker for libs/lib folder).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 1?
AThe working directory is cleaned
BThe submodule is committed
CThe submodule repo is cloned and .gitmodules is updated
DThe submodule is removed
💡 Hint
Check the 'Action' and 'Result' columns in execution_table row 1
At which step are the submodule changes staged for commit?
AStep 4
BStep 1
CStep 3
DStep 5
💡 Hint
Check the 'Action' and 'Result' columns in execution_table row 1
If you forget to commit after adding a submodule, what will happen?
AThe submodule will not be recorded in the main repo
BThe submodule will be fully integrated automatically
CGit will delete the submodule folder
DThe .gitmodules file will be removed
💡 Hint
Refer to key_moments about why committing is necessary
Concept Snapshot
git submodule add <repo_url> <path>
- Clones a repo inside your repo as a submodule
- Updates .gitmodules with submodule info
- Automatically stages .gitmodules and submodule folder; commit changes
- Submodule is tracked as a separate repo inside your repo
Full Transcript
Adding a submodule in Git means including another Git repository inside your main repository. You run 'git submodule add' with the repo URL and path. Git clones the submodule repo into the specified folder and updates a file called .gitmodules to record this. The changes are automatically staged. You then commit these changes to save the submodule info in your main repo. This process keeps the submodule as a separate project inside your repo, tracked by its commit reference. Remember to commit after adding, or the submodule won't be recorded properly.