Submodules, Subtrees & Worktrees
4 learning objectives Β· 5 key terms Β· 5 commands Β· 1 diagrams Β· 5 quiz questions
Submodules, Subtrees & Worktrees
Advanced GitWhat you'll be able to do
- Compose repositories from other repositories with submodules
- Compare submodules, subtrees, and worktrees honestly
- Work across multiple branches simultaneously with worktrees
- Update, pin, and troubleshoot nested repository setups
The idea, in plain language
Big systems outgrow single repositories, and three mechanisms compose repos from repos. Submodules nest a pinned commit of another repository inside yours (a gitlink entry plus .gitmodules config) β exact versions, explicit updates, and the famous footguns: detached HEAD checkouts, forgotten --recurse-submodules clones, and commits pointing at unpushed submodule states.
Subtrees merge another repository's history into yours (readable with plain git, updatable via split), trading submodule precision for simplicity. Worktrees solve a different problem β checking out multiple branches at once into separate directories from one clone, ending stash gymnastics when reviews interrupt. Choose per need: pinned dependencies (submodules), vendored code you modify (subtrees), parallel branches (worktrees).
Key terms
- submodule
- A nested repository pinned at one commit, recorded via gitlink plus .gitmodules.
- gitlink
- The special index entry pointing at the nested commit β what the superproject actually versions.
- subtree
- Merged foreign history living as an ordinary directory, updatable via subtree split/pull.
- worktree
- An additional checked-out working tree (branch) attached to one repository.
- --recurse-submodules
- The clone/update flag that actually fetches nested content instead of empty directories.
How it works, step by step
- 1Add a submodule
git submodule add <url> vendor/lib pins the dependency at its current commit and records the mapping in .gitmodules β commit both.
addbashgit submodule add https://github.com/acme/logger.git vendor/logger git status # shows .gitmodules + gitlink entry git commit -m "Vendor logger at v2.1"
- 2Clone with content
Plain clones leave submodule directories empty; --recurse-submodules fetches nested content, and submodule update --init --recursive repairs existing clones.
clonebashgit clone --recurse-submodules <url> # or, in an existing clone: git submodule update --init --recursive
- 3Update deliberately
Enter the submodule, pull or checkout the new version, return and commit the new gitlink β superproject and dependency advance as one reviewed change.
updatebashcd vendor/logger && git pull origin main cd ../.. && git add vendor/logger git commit -m "Bump logger to v2.2"
- 4Work in parallel with worktrees
git worktree add ../hotfix main creates a second checkout for reviews without touching current work; remove it when done.
worktreesbashgit worktree add ../hotfix origin/main git worktree list git worktree remove ../hotfix
- 5Consider subtrees for vendoring
git subtree add --prefix=vendor/lib <url> main --squash merges foreign history readably; split and pull keep it current without submodule machinery.
subtreebashgit subtree add --prefix=vendor/lib https://github.com/acme/lib.git main --squash
Commands worth memorising
Every block below is copy-ready β use the Copy button, then paste it into your terminal. Expected output is shown underneath so you know what βworkingβ looks like.
git submodule add https://github.com/acme/logger.git vendor/logger
Cloning into 'vendor/logger'... (use .gitmodules + gitlink to record it)
Commit .gitmodules and the gitlink together β one without the other breaks clones.
git clone --recurse-submodules <url>
Without the flag, submodule directories arrive empty and builds fail mysteriously.
git submodule status
a1b2c3d vendor/logger (v2.1)
Leading space means initialized and matching; -/+ flags mean uninitialized or diverged.
git worktree add ../hotfix origin/main
Preparing worktree (checking out origin/main)
A second branch checked out beside current work β no stash required.
git worktree list/repo a1b2c3d [main] /repo-hotfix e4f5a6b [hotfix]
Shows every attached tree; prune stale metadata if directories were deleted by hand.
Workflow diagrams
| Approach | Best for | Watch out for |
|---|---|---|
| Submodules | Pinned external dependencies | Detached HEAD, empty clones, unpushed gitlinks |
| Subtrees | Vendored code you modify | Split/pull discipline, squashed history |
| Worktrees | Multiple branches at once | Stale trees; prune after deleting directories |
Different problems, different tools β pinning vs vendoring vs parallel checkouts.
- Dependencies you track but rarely edit: submodules.
- Foreign code you patch regularly: subtrees.
- Simultaneous branches: worktrees, always.
Worked example
Reviewing an urgent fix while feature work stays untouched, with a vendored dependency alongside.
git worktree add ../hotfix origin/main # review + test in ../hotfix, feature work undisturbed in ./ git worktree remove ../hotfix git subtree pull --prefix=vendor/lib https://github.com/acme/lib.git main --squash
Hotfix verified without stashing; vendored library updated in place with readable history.
Where you'll use it
Common mistakes
- Cloning without --recurse-submodules, then debugging empty directories.
- Committing a gitlink pointing at an unpushed submodule commit.
- Working on a detached HEAD inside a submodule and losing the commits.
- Deleting worktree directories by hand without worktree prune.
Best practices
- Commit .gitmodules and gitlinks together, always.
- Update submodules deliberately: enter, pull, return, commit the pointer.
- Prefer worktrees over stash for parallel branches.
- Choose subtrees when you regularly modify the vendored code.
- Prune worktree metadata after manual directory deletion.
Hands-on exercise
Compose and parallelize
Nest a repo, update it deliberately, and review across worktrees.
- Create two practice repos; add one as a submodule of the other and push both.
- Clone the superproject fresh with and without --recurse-submodules; compare.
- Update the nested repo, then bump the superproject pointer in a reviewed commit.
- Open a worktree on another branch and make an isolated change there.
- Convert the pattern to a subtree in a scratch clone and compare histories.
π‘ Hint: git submodule status decodes every state: space (ok), - (uninitialized), + (diverged).
Done when: A pinned, updated, understood nesting plus one parallel-branch review without stashing.
Checkpoint quiz
0 of 5 answered Β· 0 correct
- Multiple choiceQ1
What does the superproject actually record for a submodule?
- Multiple choiceQ2
Fresh clones show empty submodule directories. What was forgotten?
- True or falseQ3
True or false: worktrees let you check out multiple branches simultaneously from one clone.
- TroubleshootingQ4
Teammates see your superproject update fail CI on missing nested files. Most likely cause?
- Multiple choiceQ5
When is a subtree preferable to a submodule?
Module summary
- Submodules pin nested repositories via gitlinks plus .gitmodules.
- Always clone/update with --recurse-submodules semantics in mind.
- Update deliberately: enter, pull, return, commit the pointer.
- Subtrees vendor modifiable code with merged history.
- Worktrees give parallel branches without stashing.
Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Put the GitHub course to work: open one of the company-wise repositories and follow the workflow you just learned, branch to release.