bashadvanced
Git Submodule Management
Add, update, and manage Git submodules for including external repositories in your project.
bashPress ⌘/Ctrl + Shift + C to copy
# 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.
bashadvanced
Git Submodules — Add, Update, and Pin
Manage repository dependencies with submodules: add, update, pin versions, and handle nested repos.
Best for: Sharing code libraries across multiple repos
#git#submodule
bashadvanced
Git Subtree Workflow
Use git subtree to include external repositories as subdirectories without submodule complexity.
Best for: Including shared code without submodule complexity
#git#subtree
bashadvanced
Git Sparse Checkout — Clone Only What You Need
Use sparse checkout to clone specific directories from large monorepos without downloading everything.
Best for: Working on a specific package in a monorepo
#git#sparse-checkout
bashintermediate
Git Hooks Pre Commit
Git workflow: git-hooks-pre-commit
Best for: version control
#git#version-control