Git Submodules Guide
This guide explains how to manage dependent repositories (<submodule1> and <submodule2>) inside the main <project> repo using Git submodules.
1️⃣ Clone the Main Repo
- Clones only the main repo. Submodules are not fetched yet.
2️⃣ Add Dependent Repos as Submodules
- This creates
.gitmodulesautomatically. .gitmodulesmaps the submodule folder paths to the repo URLs.
.gitmodules content:
3️⃣ Commit Submodules in Main Repo
- This stores the submodule references (commit pointers) in the main repo.
- Important: It does not include the actual submodule code in the main repo.
4️⃣ Workflow for Making Changes
A. Make changes in a submodule
B. Update the main repo pointer
C. Make changes in the main repo
- Edit files outside submodule folders as usual:
- Submodule references remain unchanged unless you explicitly update them.
5️⃣ Pulling Updates
A. Pull main repo updates
- Updates main repo content and submodule commit pointers (but not submodule content).
B. Pull submodule updates
--init --recursive→ fetches submodules after a fresh clone.--remote --merge→ updates submodules to latest commits from their remote repos.
6️⃣ Fresh Clone with Submodules
To clone and have submodules ready automatically:- This populates
<submodule1-folder>and<submodule2-folder>folders.
7️⃣ Optional: Setup Script
You can create asetup.sh for developers:
8️⃣ Key Notes
- Submodule commits are independent — changes inside submodules must be committed and pushed inside the submodule repo.
- Main repo only tracks submodule commit pointers.
.gitmodulescontains mapping info: submodule path → repo URL.- Fresh clones require
git submodule update --init --recursiveto populate submodules. - Use
git submodule update --remote --mergeto sync submodules with the latest remote commits.

