Work on multiple branches simultaneously with multiple working directories
The wit worktree command lets you check out multiple branches simultaneously in separate directories. This is useful when you need to work on different branches without constantly switching.
# Create worktree for an existing branchwit worktree add ../feature-worktree feature-branch# Create worktree with a new branchwit worktree add ../hotfix-worktree -b hotfix/urgent-fix# Create worktree in a subdirectorywit worktree add worktrees/experiment experiment-branch
Preparing worktree (checking out 'feature-branch')HEAD is now at abc1234 Add user authenticationWorktree created at: /path/to/project/../feature-worktree
You’re working on a feature when an urgent bug needs fixing:
Copy
# You're in your project, working on feature-branch# Don't stop what you're doing - create a new worktreewit worktree add ../hotfix maincd ../hotfix# Create and fix the hotfix branchwit switch -c hotfix/critical-bug# ... make your fixes ...wit commit -a -m "Fix critical bug"wit push -u origin hotfix/critical-bug# Create PR, get it reviewed and merged# Go back to your feature workcd ../project# Your changes are exactly as you left them!# Clean up the hotfix worktreewit worktree remove ../hotfix
# Create worktrees for different versionswit worktree add ../v1 v1.0.0wit worktree add ../v2 v2.0.0# Now you can diff, compare, or run tests on bothdiff ../v1/src/api.ts ../v2/src/api.ts# Clean up when donewit worktree remove ../v1wit worktree remove ../v2
Run tests on one branch while continuing to develop:
Copy
# Create a worktree for testingwit worktree add ../test-runner main# In terminal 1: Run continuous testscd ../test-runnernpm run test:watch# In terminal 2: Continue developmentcd ../project# Make changes, tests run automatically in the other terminal
Review a colleague’s PR without switching branches:
Copy
# Create worktree for the PR branchwit worktree add ../pr-review feature/their-featurecd ../pr-review# Review the code, run it, test it# When done, clean upcd ../projectwit worktree remove ../pr-review
All worktrees share the same .wit (or .git) directory:
Copy
project/ .wit/ # Shared repository data worktrees/ # Worktree metadata feature-worktree/ # Links to the feature worktree src/ # Main worktree filesproject-feature/ # Feature worktree .wit -> ../project/.wit # Link back to shared data src/ # Feature branch files
This means:
Objects, refs, and history are shared (saves disk space)
Commits made in any worktree are immediately visible to all
If you see “fatal: ‘branch’ is already checked out”:
Copy
# Check which worktree has the branchwit worktree list# Either remove that worktree or use a different branchwit worktree remove ../other-worktree# orwit worktree add ../new-worktree different-branch
A branch can only be checked out in one worktree at a time.
Worktree directory exists
If the target directory already exists:
Copy
# Remove or rename the existing directoryrm -rf ../existing-dir# ormv ../existing-dir ../existing-dir-backup# Then create the worktreewit worktree add ../existing-dir my-branch
Stale worktree after manual deletion
If you deleted a worktree directory manually:
Copy
# Clean up the stale referencewit worktree prune
Uncommitted changes in worktree
If wit worktree remove fails due to uncommitted changes:
Copy
# Either commit/stash the changescd ../worktree-to-removewit stashcd ../main-projectwit worktree remove ../worktree-to-remove# Or force remove (loses changes!)wit worktree remove --force ../worktree-to-remove