You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Think of a **submodule** as another Git repository (like a library or dependency) that lives inside your main repository. Instead of copying code, you **reference it**. The main repo records *which commit* of the submodule it should use. This ensures everyone on the team is working with the **exact same version**.
Analogy: If your project is a house, a submodule is a prefabricated room you install inside. You don’t build it from scratch — you bring it in at a certain stage of completion.
### Key Git Terms
* **Commit** – A snapshot of code at a point in time.
* **Tag** – A label pointing to a specific commit, often used for version numbers like v1.2.3.
* **Branch** – A moving line of development (e.g., main).
* **Detached HEAD** – A state where Git points directly to a commit (like when you checkout a tag) rather than tracking a branch.
* **.gitmodules** – A file in your repo that records where each submodule lives and its source URL.
---
## 2. The JSON Manifest (submodules.json)
The manifest defines which submodules your project needs. Example:
3. Commit the updated manifest and .gitmodules so your team can pull it.
---
## 5. Git Nuances You’ll See
* **Detached HEAD**: When checking out a tag, Git puts the submodule into this state. It’s normal and safe. It just means the submodule is at an exact commit, not following a branch.
* **Pinning**: Even if you checkout v1.2.3, the main repo actually records the commit hash behind that tag. This ensures reproducibility.
* **Constraints**: The script can look at available tags and pick the highest one that fits a rule (e.g., >=1.0.0). This mimics package managers like npm or pip.
* **Committing**: Each time you install, update, or remove, you usually add -Commit so the parent repo records the changes. Otherwise, they stay local.
* **.gitmodules File**: Git automatically updates this file when you add or remove submodules. It should be versioned.
* Always commit and push after making changes so teammates get the new state.
* Prefer tags (v1.0.0) over branches (main) for stability.
* Keep all submodules under one folder (submodules/) for consistency.
* Document new submodules in the JSON manifest, not just .gitmodules.
---
## 8. Future Enhancements
* **Lock file**: Save exact commits resolved from constraints.
* **Dry run**: Preview changes before applying.
* **CI/CD integration**: Auto-run restore in pipelines.
* **Caching**: Speed up repeated submodule fetches.
---
With this system, submodules behave like a lightweight package manager: you declare dependencies in JSON, and use simple commands to install, restore, update, or remove them.