Git — worktrees
Complex Multiple working directories, one.git. Perfect for PR reviews, hotfixes, and AI agents.Why
| Scenario | Without worktree | With worktree |
|---|---|---|
| Hotfix while feature WIP | stash or commit WIP | second folder on main |
| Agent on a branch | fights your dirty tree | isolated path |
| Compare two branches | constant switch | side-by-side |
List & add
bash
git worktree list
# new branch in sibling folder
git worktree add ../repo-hotfix -b hotfix/payment main
# existing branch
git worktree add ../repo-feature feature/loginWork & push as usual
bash
cd ../repo-hotfix
# edit, commit, push
git push -u origin HEADRemove
bash
# after branch is merged / done
git worktree remove ../repo-hotfix
# if folder already deleted:
git worktree pruneRules of thumb
- One branch per worktree — don’t check out the same branch in two trees
- Keep agent worktrees under a convention, e.g.
../repo-.worktrees/name - Clean up aggressively — worktrees accumulate disk
Useful combo
bash
git fetch origin
git worktree add ../repo-pr-123 origin/pr/123
# review, then removeLinked notes
- Everyday branch flow
- Agent CLIs often spawn worktrees: Claude Code, Codex