To effectively commit changes in submodules while maintaining synchronization with the parent repository, follow this structured approach:
cd path/to/submodule # Navigate into the submodule directory
git checkout -b feature/your-change # Create a new branch (recommended)
# Make your code changes...
git add .
git commit -m "Your commit message"
git push origin feature/your-change # Push to the submodule's remote
- Submodules are independent repositories. Commit here first before updating the parent repo.
- Use a branch to avoid detached HEAD state.
After committing/pushing the submodule changes:
cd ../.. # Return to the parent repo
git add path/to/submodule # Stage the submodule's new commit hash
git commit -m "Update submodule to latest commit"
git push origin main # Push parent repo's reference update
- The parent repo tracks the submodule's commit hash, not branches.
- Staging the submodule (
git add
) locks in the new commit.
Others must update both parent and submodules:
git pull # Pull parent repo changes
git submodule update --init --recursive # Sync submodules to new commits
To track a branch (instead of a fixed commit):
- Update
.gitmodules
:[submodule "submodule-name"] path = path/to/submodule url = https://github.com/user/repo.git branch = main # Add this line
- Sync:
git submodule sync git submodule update --remote # Pull latest from the branch
Scenario | Command | Purpose |
---|---|---|
Submodule Development | cd submodule && git checkout -b feature |
Isolate changes in a branch. |
Update Parent Reference | git add submodule && git commit |
Lock in submodule's new commit. |
Pull Latest Submodule | git submodule update --remote |
Fetch branch-based updates. |
Recursive Clone | git clone --recurse-submodules |
Clone repo + submodules in one step. |
- Detached HEAD?
Run inside submodule:git checkout main && git pull
- Forgot to Commit Submodule First?
Parent repo will reference an uncommitted submodule state. Fix:cd submodule && git commit -m "Emergency fix" && cd .. git add submodule
sequenceDiagram
participant ParentRepo
participant Submodule
participant Remote
Submodule->>Submodule: git commit -m "Change"
Submodule->>Remote: git push origin branch
ParentRepo->>ParentRepo: git add submodule
ParentRepo->>ParentRepo: git commit -m "Update submodule"
ParentRepo->>Remote: git push origin main
- Commit in Submodule First: Treat submodules as standalone repos.
- Then Update Parent: The parent repo pins the submodule's commit.
- Track Branches Carefully: Use
--remote
for dynamic updates. - Communicate Changes: Team members must run
git submodule update
.
This ensures clean, traceable history in both parent and submodule repos.