Worktrees let you check out multiple branches of the same repo into different directories, without stashing or switching back and forth.
Perfect for: hotfixes, reviews, long tests, parallel dev or parallel AI coding (with Codex CLI, Claude Code, etc.).
From your repo root:
# make a sibling folder for worktrees
mkdir -p ../myrepo-wt
git fetch --all -pNew feature branch from main:
git worktree add -b feat/x ../myrepo-wt/feat-x origin/main
cd ../myrepo-wt/feat-x➡️ Now you have two working dirs:
./→ main repo (your normal workspace)../myrepo-wt/feat-x→ feature branch workspace
Both share the same .git database.
Parallel task (no stash needed):
git worktree add -b hotfix/ssl ../myrepo-wt/hotfix-ssl origin/releaseCode review (clean checkout):
git fetch origin pull/123/head:pr/123
git worktree add ../myrepo-wt/pr-123 pr/123Long-running tests / perf experiments:
git worktree add ../myrepo-wt/tests origin/mainPush only the current branch (avoid leaking all branches):
git push origin HEAD --follow-tagsWhen a branch is merged:
git worktree remove ../myrepo-wt/feat-x
git branch -d feat/x
git worktree prune --expire=now- One branch per worktree — you can’t check out the same branch twice.
- Each worktree = one purpose (feature, review, hotfix, test).
- Don’t
git push --allcasually — useHEADunless you really want to mirror. - Prune old trees regularly with
git worktree prune.
git worktree list
git worktree add -b <branch> ../repo-wt/<branch> [start-point]
git worktree add ../repo-wt/tmp --detach <commit>
git worktree remove ../repo-wt/<branch>
git worktree prune --expire=now.git (shared history & branches)
├── main-repo/ (main workspace)
└── repo-wt/
├── feat-x/ (feature branch)
├── hotfix-ssl/ (hotfix branch)
└── pr-123/ (code review)
Branches and commits sync instantly between worktrees.
Each folder has its own working dir, build artifacts, editor settings, etc.
ONE-LINER TO REMEMBER:
git worktree add -b <branch> ../repo-wt/<branch> origin/main→ parallel workspace, no stash needed.