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

CommandWhat it does
git worktree add ../hotfix mainCheck out main in a second directory
git worktree listShow all linked working trees
git worktree remove ../hotfixRemove a linked tree
git submodule update --init --recursiveClone nested submodules after clone
git submodule update --remotePull 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.

よくある質問

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