How to git submodule tutorial |Video upload date:  · Duration: PT24M59S  · Language: EN

Learn how to add manage and update git submodule with clear steps and examples for reliable nested repositories and cleaner workflows

Welcome to the submodule jungle

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.

When to use submodules and when to avoid them

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.

Add a submodule

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.

Initialize and fetch submodule data

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

Clone including submodules

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.

Update and change submodule commits

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.

Remove a submodule cleanly

Removing a submodule needs more care than deleting a folder. Do the deinit and remove steps so no ghost entries haunt your repo.

  1. Deinitialize the submodule so git stops tracking it
  2. Remove the module directory under .git so git internals do not keep stray data
  3. Remove the path from the index and commit the change in the parent 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.

Tips to stay out of submodule hell

  • Pin submodule commits to known good states and document the update process in the parent repo
  • Use clear paths and sensible branch names inside submodules so teammates are not asking for the meaning of life
  • Include the git submodule update step in CI or build docs so pipelines do not fail unexpectedly
  • Consider alternatives like subtrees or package registries if you want fewer manual steps

Final thoughts

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.