patterngitTip
git worktree: check out multiple branches simultaneously
Viewed 0 times
worktreemultiple branchesparallel checkoutworktree addworktree remove
Error Messages
Problem
Switching branches to review a PR or fix a hotfix interrupts active development work. Stashing and switching is slow and error-prone.
Solution
Use git worktree to check out a second branch in a separate directory without disturbing the current working tree:
# Create a new worktree for a hotfix
git worktree add ../repo-hotfix hotfix/critical-fix
# Work in the separate directory
cd ../repo-hotfix
# ... make changes, commit, push ...
# List active worktrees
git worktree list
# Remove the worktree when done
git worktree remove ../repo-hotfix
# Create a new worktree for a hotfix
git worktree add ../repo-hotfix hotfix/critical-fix
# Work in the separate directory
cd ../repo-hotfix
# ... make changes, commit, push ...
# List active worktrees
git worktree list
# Remove the worktree when done
git worktree remove ../repo-hotfix
Why
Each worktree has its own working directory and index but shares the same .git directory and object store. Branches checked out in one worktree are locked and cannot be checked out in another.
Gotchas
- A branch can only be checked out in one worktree at a time — attempting to check out an active worktree branch errors out
- Worktrees created from bare repositories require a slightly different path convention
git worktree pruneremoves stale worktree metadata after manually deleting the directory
Code Snippets
Working with multiple branches via git worktree
# Add a worktree for a new branch
git worktree add ../my-repo-feature feature/new-ui
# Add a worktree for an existing remote branch
git worktree add ../my-repo-hotfix -b hotfix/fix origin/hotfix/fix
# List all worktrees
git worktree list
# Clean up
git worktree remove ../my-repo-feature
git worktree pruneContext
When context switching between features frequently or when reviewing PRs that require running code
Revisions (0)
No revisions yet.