Git Worktree and Submodule
Work on multiple branches at once and manage nested repos.
- git worktree
- git submodule
- multiple branches
- nested repo
- worktree remove
Git Worktree and Submodule
Work on multiple branches at once and manage nested repos.
Commands
| Command | What it does |
|---|---|
git worktree add ../hotfix main | Check out main in a second directory |
git worktree list | Show all linked working trees |
git worktree remove ../hotfix | Remove a linked tree |
git submodule update --init --recursive | Clone nested submodules after clone |
git submodule update --remote | Pull latest upstream into submodules |
Example
git worktree add ../hotfix main
cd ../hotfix && git pull
git worktree list
FAQ
Worktree vs a second clone?
Worktrees share one .git dir, so fetches and objects are shared; separate clones duplicate everything.
Submodule vs subtree?
Submodules pin exact commits of another repo; subtrees merge the code in. Prefer submodules for vendor deps.
Soalan Lazim
Worktree vs a second clone?
Worktrees share one .git dir, so fetches and objects are shared; separate clones duplicate everything.
Submodule vs subtree?
Submodules pin exact commits of another repo; subtrees merge the code in. Prefer submodules for vendor deps.
Git
Git Config Essentials
Set identity, defaults, and aliases for Git on a new machine.
Git Clone and Init
Start a repo from scratch or copy a remote repository locally.
Git Add and Commit
Stage changes and record them as commits with clear messages.
Git Branch Basics
Create, list, rename, and delete branches for parallel work.