bashadvanced

Git Submodule Management

Add, update, and manage Git submodules for including external repositories in your project.

bash
# Add a submodule
git submodule add https://github.com/org/lib.git vendor/lib
git commit -m "Add lib submodule"

# Clone a repo with submodules
git clone --recurse-submodules https://github.com/org/project.git

# Initialize submodules after cloning
git submodule init
git submodule update

# Or both at once
git submodule update --init --recursive

# Update submodule to latest remote commit
cd vendor/lib
git checkout main
git pull
cd ../..
git add vendor/lib
git commit -m "Update lib submodule to latest"

# Update all submodules at once
git submodule update --remote --merge

# Check submodule status
git submodule status

# Remove a submodule
git submodule deinit -f vendor/lib
rm -rf .git/modules/vendor/lib
git rm -f vendor/lib
git commit -m "Remove lib submodule"

# Foreach: run command in all submodules
git submodule foreach 'git checkout main && git pull'

Use Cases

  • Including shared libraries across multiple repos
  • Pinning external dependencies to specific versions
  • Managing monorepo-like structures

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.