How to Use the Git Submodule Update Command by Example |Video upload date:  · Duration: PT5M41S  · Language: EN

Learn how to use git submodule update with init recursive and remote flags and fix common detached HEAD and sync problems.

Practical guide to keeping submodules in sync and fixing common git issues

If your repository uses submodules and you enjoy mild chaos, welcome. This friendly git tutorial walks through the git submodule update command with examples you can actually copy and paste without crying. We will cover initialization, updating to recorded SHAs, following branch tips on remotes, recovering from detached HEAD states, and running maintenance across all submodules. All the usual git commands are here and none of them bite, probably.

Start by initializing submodules after a clone

When you clone a repo that uses submodules you might find empty folders or mysterious metadata. That is intentional suffering. Fix it by registering and checking out the submodule entries so nested projects show up like they are supposed to.

git submodule init
git submodule update --init --recursive

The init step registers the submodule entries. The update part actually checks out the nested content, recursively. If you skip this you will be wondering why your build fails and blaming unhelpful things like the internet.

Update submodules to the recorded commits

For reproducible builds you usually want each submodule at the exact commit the parent repository expects. That is what git submodule update does. It moves submodules to the recorded SHAs so things behave like they did when whoever broke the build last tested locally.

git submodule update

Run that after switching branches or after pulling changes to the parent repo. It ensures the submodule working trees match the superproject reference.

Follow branch tips from submodule remotes

If you prefer tracking a branch in a submodule instead of pinning to a commit, use the remote option. This will fetch from the submodule remote and merge the tracked branch so your submodule stays current with upstream work.

git submodule update --remote --merge

Use this when you want the submodule to follow a branch tip rather than freeze time and pretend everything is stable.

Recover from a detached HEAD inside a submodule

Sometime after cloning a repo your submodule might be in a detached HEAD state. That sounds dramatic but it is just git telling you it checked out a commit not a branch. The fix is mundane.

cd path/to/submodule
git checkout main

Then run the init update command again if needed

git submodule update --init --recursive

If your project uses a branch other than main replace main with the branch you actually track. Yes that includes the occasional legacy branch like master. No judgment, only mild sarcasm.

Run maintenance across all submodules

Want to perform the same command in every submodule without hand editing 37 terminals? git submodule foreach is your tiny automation hero. It runs a shell command inside each submodule in sequence.

git submodule foreach "git fetch && git checkout main && git pull"

This example fetches remotes, checks out main, and pulls. Replace main with whatever branch you track. You can also run linting or tests from here if you like living dangerously.

Troubleshooting checklist for submodule problems

  • Folder looks empty: run git submodule update --init --recursive to populate it.
  • Submodule at wrong commit: run git submodule update to sync to recorded SHAs.
  • Want to track a branch: use git submodule update --remote --merge to fetch and merge.
  • Detached HEAD in submodule: cd into it and git checkout the correct branch, then update the superproject.
  • Keep remotes in sync: use git submodule foreach to script fetch and pull across all nested repos.

Quick notes on workflow and best practices

Declare the branch you want in the parent repo if you want others to follow a branch tip. Commit submodule pointer updates in the parent repository so collaborators do not get surprised. Prefer explicit commands for reproducibility and use --remote when you actually want live updates from submodule remotes.

Recap and final tips

Initialization for new clones, update to recorded SHAs for reproducibility, follow remotes when you want live tips, and fix detached HEADs with a checkout plus an update. Use git submodule foreach for mass maintenance. These few git commands remove a lot of the mystery and reduce the hours spent debugging nested repos while pretending to be productive.

If you run into odd errors check the submodule remotes with git remote -v and verify the branch exists upstream. If nothing helps take a break then try again with coffee and slightly more patience.

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.