Git submodules are the neat way to nest one repository inside another when you want shared libraries or components without copying files like a peasant. They are also the kind of thing that can ruin your afternoon if you do not document the workflow. This guide walks through adding initializing cloning updating and removing submodules while keeping your sanity and your CI build working.
Use a git submodule when you need a clear boundary between projects and you want to pin a dependency to a specific commit. Avoid submodules if you and your team prefer magic auto updates from package managers or if you hate manual steps. Submodules are great for shared components or tools that need exact version control for reproducible builds.
Pick a path inside the parent repo and tell git to add the nested repository. This records the remote and sets up a pointer to the submodule commit. Think of this as putting a leash on a small repo that you want to track.
git submodule add REPO_URL PATH
That creates an entry in the .gitmodules file and sets up the nested repo at PATH. Commit the .gitmodules and the PATH change to the parent repo so others see the relationship.
After adding a submodule or after checking out a branch that mentions submodules run these commands so git registers and downloads the nested repository data. This step prevents mysterious build failures later.
git submodule init
git submodule update
If you clone a project that uses nested repositories do not be lazy. Populate submodules during the clone to avoid surprises in tests or builds. You can clone first then populate or do it in two steps if you like to watch progress.
git clone REPO_URL
git submodule update --init --recursive
The --recursive flag handles submodules inside submodules. Yes that is a thing. Yes it can be awkward. Move on.
Submodules track specific commits. To change which commit the parent repo records go into the submodule update or switch branches then commit the change in the parent.
cd PATH
git checkout BRANCH
git pull
# or checkout a specific commit
# git checkout COMMIT_HASH
cd ..
git add PATH
git commit -m "Update submodule reference"
This stages the new recorded commit for the submodule in the parent repository. Push both repos if you want teammates to stop yelling at you.
Removing a submodule needs more care than deleting a folder. Do the deinit and remove steps so no ghost entries haunt your repo.
git submodule deinit -f PATH
rm -rf .git/modules/PATH
git rm -f PATH
# edit .gitmodules to remove the entry if needed
# commit the removal in the parent repo
After that clean up .gitmodules and commit the edits. If anyone else still has the old data they may need to run the submodule init commands again.
Git submodules give you explicit control over nested repositories which is great for reproducible builds and shared components. They do add complexity to your git workflow so communicate the process and keep the commands handy. If you follow these steps you will reduce surprises and keep the repo drama to a tolerable level.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.