How to Use the Git Submodule Init and Update Command by Exam |Video upload date:  · Duration: PT3M2S  · Language: EN

Learn how to initialize and update Git submodules with commands and examples to fetch nested repositories and keep them synced

Why submodules exist and why they annoy you

Git submodule is a way to embed one repository inside another. It is great for sharing a library across projects but it also creates an extra layer of surprises when you clone a repo and the nested repositories are missing. If you want predictable nested repositories you need to know the init and update commands and a few best practices from this devops friendly tutorial.

Initialize submodules without a surprise network hit

Running git submodule init tells your local Git about the submodule paths listed in the superproject. It does not fetch any data. That means no bandwidth and no immediate changes to your working tree. Think of it as setting up an index card with the address of the house you are about to visit.

git submodule init

Fetch and checkout the exact recorded commit

To actually download the nested repository files run git submodule update. This command fetches the commits that the superproject points at and checks out the exact commit inside the submodule working tree. That exact commit behavior is why many submodules appear in a detached HEAD state. This is intentional. It ensures the superproject reproduces the same nested state every time.

git submodule update
# combine setup and fetch for all nested repos
git submodule update --init --recursive

When you need a branch instead of a fixed commit

Submodules record commits not branches. If you want the submodule to follow a branch you have two options. Either enter the submodule and switch to the branch then pull, or use the remote option to update from the configured branch. The commands are simple and boring but effective.

# update submodules to track the configured remote branch
git submodule update --remote

# or inside the submodule do this
cd path/to/submodule
git fetch
git checkout main
git pull

Clone right the first time

When cloning a repository that contains submodules use recursion so you do not have to run extra steps later. This is a tiny trick that saves you from manual fiddling and mild profanity in team chats.

git clone --recurse-submodules <repository-url>

Quick troubleshooting tips

  • Run git submodule status to see which submodules are at detached commits or out of sync
  • Use git submodule update --init --recursive as a one line cure for missing nested content
  • If a submodule needs to move to a branch enter its folder and run fetch and checkout as shown above

Summary for people who skim

Register submodules locally with git submodule init. Fetch and check out the recorded states with git submodule update. Combine init and fetch and handle nested submodules with --init --recursive or avoid manual steps by cloning with --recurse-submodules. Remember that submodules point to commits not branches so updating to a branch requires extra steps or the --remote option. Use git submodule status for a quick health check. Now go forth and manage nested repositories like a slightly less chaotic human.

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.