Git worktree management with the wt script for simplified create, list, switch, and delete operations. Includes comprehensive guidance on best practices and troubleshooting.
Provides a streamlined wt command-line tool and comprehensive guidance for git worktree operations. The wt script handles the fiddly parts (creating, switching, deleting) while this guide covers concepts, best practices, and troubleshooting.
wt ScriptThe easiest way to manage worktrees is with the wt command:
# List all worktrees
wt list
# Create a new worktree
wt create DEV-123
# Switch to a worktree
wt switch DEV-123
# Delete a worktree (and its branch)
wt delete DEV-123
Setup: See INSTALL.md for installation instructions.
wt ScriptThe wt script can be customized to match your workflow. Configuration is optional - everything works with sensible defaults.
Configuration file: ~/.config/wt/config
The config file is created automatically on first use. You can view, edit, or reset it using:
# Show current configuration
wt config show
# Edit configuration in your editor
wt config edit
# View config file location
wt config path
# Reset to defaults (removes config file)
wt config reset
| Option | Default | Description |
|---|---|---|
EDITOR |
code |
Editor to open with --code or --go flags. Examples: cursor, zed, nvim, vim |
EDITOR_FLAGS |
(empty) | Additional flags to pass to editor. Examples: "--remote=wsl", "--wait" |
AUTO_INSTALL |
true |
Automatically install dependencies after creating worktree |
DELETE_BRANCH_PROMPT |
true |
Prompt before deleting branch. Set to false to delete without asking |
PACKAGE_MANAGER |
(auto-detect) | Override package manager detection. Options: bun, npm, yarn, pnpm |
WORKTREE_NAME_PATTERN |
{repo}-{branch} |
Pattern for worktree directory names. Variables: {repo}, {branch} |
Example 1: Using Cursor editor with WSL
EDITOR="cursor"
EDITOR_FLAGS="--remote=wsl"
Example 2: Skip dependency installation
AUTO_INSTALL="false"
Example 3: Custom worktree naming
# Just use branch name: feature-123 instead of repo-feature-123
WORKTREE_NAME_PATTERN="{branch}"
# Prefix with "wt": wt-feature-123
WORKTREE_NAME_PATTERN="wt-{branch}"
Example 4: Force package manager
# Always use bun, even if npm is detected
PACKAGE_MANAGER="bun"
wt script for everyday worktree operations (fastest path)What are git worktrees? Git worktrees allow you to have multiple working directories for the same repository. Each worktree has its own branch checked out, enabling you to work on different features simultaneously without constantly switching contexts or stashing changes.
Why use worktrees?
git worktree commands (built into git)Each worktree must have a unique branch checked out. Git prevents the same branch from being checked out in multiple worktrees simultaneously. If you need to work on the same code in multiple places, create temporary branches.
Use git worktrees when:
node_modules or build artifactsUse git stash instead when:
Use git checkout when:
When you use this skill, it will automatically detect:
DEV-123 (JIRA), feature/name (Git Flow), username/branch (Personal)master or mainThis allows the skill to provide commands and examples that match your specific repository setup.
Scenario: You're working on DEV-123 but need to start DEV-456 urgently.
Step-by-Step Guide:
# Step 1: Determine what you're creating
# - New branch from master? Use: git worktree add -b <new-branch>
# - Existing remote branch? Use: git worktree add <branch>
# Example for creating a NEW branch (most common):
git worktree add -b DEV-456 ../repo-DEV-456 origin/master
# Example for EXISTING remote branch:
git worktree add ../repo-DEV-789 origin/DEV-789
# Step 2: Navigate to the new worktree
cd ../repo-DEV-456
# Step 3: Install dependencies (required for each worktree)
npm install
# Step 4: Start development
# Your changes to DEV-123 remain untouched in the other worktree
Why each step matters:
-b DEV-456: Creates the branch and worktree together (cleaner than two commands)../repo-DEV-456: Naming convention keeps worktrees organizednpm install: Each worktree has its own node_modules (essential!)Disk space note: ⚠️ Each worktree needs ~500MB for node_modules. Having 3-4 worktrees uses 2GB+ disk space.
Scenario: You want to run a Storybook build in one worktree while coding in another.
Step-by-Step:
# In your main development worktree (Terminal 1):
cd ~/path/to/repo
npm run build-storybook # Long-running build process
# In another terminal (Terminal 2):
cd ~/path/to/repo-DEV-123
# Continue developing, unblocked by the build in Terminal 1
Why this works:
node_modules, build artifacts, etc.Pro tip: Open each worktree in a separate VS Code window to avoid IDE confusion:
code ~/path/to/repo
code ~/path/to/repo-DEV-123
Scenario: A teammate asks you to review their PR branch (DEV-789), but you're in the middle of DEV-123.
Step-by-Step:
# Step 1: Fetch the latest from remote
git fetch origin
# Step 2: Create a worktree for the PR branch
git worktree add ../repo-review origin/DEV-789
# Step 3: Navigate and test
cd ../repo-review
npm install
npm run storybook # View their work
# Test functionality, read code
# Step 4: Return to your work (in original worktree)
cd ../repo
# Your DEV-123 changes are exactly where you left them
# Step 5: Clean up the review worktree
cd ../repo
git worktree remove ../repo-review
Why this matters:
Scenario: You want to compare your approach (DEV-100) with a teammate's approach (DEV-101).
Step-by-Step:
# Create both worktrees
git worktree add -b DEV-100 ../repo-approach-a origin/master
git worktree add -b DEV-101 ../repo-approach-b origin/master
# Open both in separate editor windows
code ../repo-approach-a
code ../repo-approach-b
# Now you can:
# - Compare file implementations side-by-side
# - Test both approaches
# - See which performs better
# - Make a decision
# Keep the better approach, remove the other
git worktree remove ../repo-approach-b
Naming Convention:
repo/ # Main worktree (current work)
repo-DEV-123/ # Feature branch
repo-DEV-456/ # Another feature
repo-master/ # Optional: clean master for comparisons
Why this helps:
Critical: Each worktree needs its own dependencies installed.
# After creating a worktree
git worktree add -b DEV-123 ../repo-DEV-123 origin/master
cd ../repo-DEV-123
npm install # ⚠️ Essential - don't skip this!
Lock file considerations:
package-lock.json is shared across worktrees (lives in .git space)npm install againExample:
# In worktree DEV-123, you upgrade a package
npm install lodash@latest
# This changes package-lock.json (shared)
git add package-lock.json
git commit -m "DEV-123: upgrade lodash"
# In worktree DEV-456, you need to sync:
cd ../repo-DEV-456
git pull origin master # Get the lock file update
npm install # Update node_modules to match
Monitor worktree disk usage:
# See all worktrees and their size impact
git worktree list
# Clean up node_modules from unused worktrees
cd ../unused-worktree
rm -rf node_modules # Before removing the worktree
# Remove worktree
cd ../main-worktree
git worktree remove ../unused-worktree
Budget:
Decision flowchart:
How long will you work on this branch?
│
├─ < 5 minutes → Use: git checkout (simple switch)
│
├─ 5-15 minutes → Use: git stash (temporary switch)
│
└─ > 15 minutes → Use: worktree (serious parallel work)
Practical example:
# Quick fix (< 5 min) - just checkout
git checkout master
# Fix the thing
git checkout DEV-123
# Temporary review (5-15 min) - use stash
git stash
git checkout DEV-789 # Review PR
git checkout DEV-123
git stash pop
# Parallel development (> 15 min) - use worktree
git worktree add -b DEV-456 ../repo-DEV-456 origin/master
# Serious work in both places
# Create worktree with a NEW branch
git worktree add -b <new-branch> ../<dir-name> origin/master
# Example:
git worktree add -b DEV-123 ../repo-DEV-123 origin/master
# Create worktree from EXISTING remote branch
git worktree add ../<dir-name> origin/<branch-name>
# Example:
git worktree add ../repo-DEV-456 origin/DEV-456
# See all active worktrees
git worktree list
# Output example:
# /path/to/repo abc1234 [master]
# /path/to/repo-DEV-123 def5678 [DEV-123]
# /path/to/repo-DEV-456 ghi9012 [DEV-456]
# Simple terminal approach
cd ../repo-DEV-123
# Editor approach (preferred)
code ../repo-DEV-123
# VS Code: Opens new window for the worktree
# Safe removal (must run from DIFFERENT worktree)
cd ../repo
git worktree remove ../repo-DEV-123
# Force removal (⚠️ use only if you're sure)
git worktree remove --force ../repo-DEV-123
# Clean up stale references
git worktree prune
# Fetch latest changes (affects all worktrees)
git fetch origin
# In a specific worktree, update local branch
cd ../repo-DEV-123
git pull origin master # Merge master into your branch
# or
git rebase origin/master # Rebase your work on latest master
What happened:
git worktree add ../repo-DEV-123 DEV-123
# Error: 'DEV-123' is already checked out at '/path/to/repo'
Why: Git prevents the same branch from being in multiple worktrees. This is a safety feature.
Solutions:
Option 1: Remove the other worktree
git worktree remove ../repo
# Then create new worktree with that branch
Option 2: Create a temporary branch
git worktree add -b DEV-123-temp ../repo-DEV-123-temp DEV-123
# Now you have DEV-123 in two places (original + temp)
Option 3: Check where it's checked out
git worktree list # Shows all locations
What happened:
git worktree remove ../repo-DEV-123
# Error: cannot remove working tree - have uncommitted changes
Why: Git protects your work by refusing to remove worktrees with unsaved changes.
Solutions:
Option 1: Commit the changes (recommended)
cd ../repo-DEV-123
git add .
git commit -m "DEV-123: save work before removing worktree"
cd ../repo
git worktree remove ../repo-DEV-123
Option 2: Stash the changes
cd ../repo-DEV-123
git stash
cd ../repo
git worktree remove ../repo-DEV-123
Option 3: Force remove (⚠️ CAUTION - loses work)
git worktree remove --force ../repo-DEV-123
# This deletes uncommitted work permanently
What happened:
cd ../repo-DEV-123
npm install
# Takes forever or has permission errors
Why:
node_modules is separate (slow)Solutions:
Option 1: Ensure git fetch is done first
git fetch origin # Do this once, benefits all worktrees
cd ../repo-DEV-123
npm install # Should be faster now
Option 2: Check npm cache
npm cache verify # Fix potential cache issues
npm install
Option 3: Use npm ci instead of npm install
npm ci # Cleaner install, respects lock file exactly
What happened: Your IDE shows files as changed/deleted because you're working in worktree DEV-123 but the IDE thinks you're in DEV-456.
Why: IDEs can get confused when multiple worktrees are open in the same editor.
Solutions:
Option 1: Open separate VS Code windows (recommended)
code ../repo # Window 1
code ../repo-DEV-123 # Window 2
Each window gets its own git/file context.
Option 2: Close other worktree windows Close any IDE windows pointing to other worktrees while working.
Option 3: Use workspace settings
Create .vscode/settings.json in each worktree with distinct settings.
What happened:
You deleted a worktree directory manually (not using git worktree remove), and now git worktree list shows it.
Symptoms:
git worktree list
# Shows /path/to/repo-DEV-123 (broken link)
Solution:
# Clean up stale references
git worktree prune
# Verify cleanup
git worktree list
What happened: You updated dependencies in DEV-123, now DEV-456 has lock file conflicts.
Why:
Lock files are shared, but node_modules are separate per worktree.
Solution:
# In DEV-123 (where you made changes)
git add package-lock.json
git commit -m "DEV-123: update dependencies"
git push origin DEV-123
# In DEV-456 (affected worktree)
git fetch origin
git pull origin master # Get the updated lock file
npm install # Resync node_modules with new lock file
Prevention: Commit lock file changes immediately when you update dependencies.
Do you need to work with multiple branches?
│
├─ "I need to switch between 2 branches repeatedly"
│ ├─ Will take < 5 min each switch? → Use: git checkout
│ └─ Will take > 5 min each switch? → Use: git worktree
│
├─ "I need to test/compare two implementations"
│ └─ Create 2 worktrees, open side-by-side
│
├─ "I have a long build running"
│ └─ Use: git worktree (separate build in separate worktree)
│
├─ "I need to review a PR"
│ └─ Use: git worktree (create temp worktree for review)
│
├─ "I have uncommitted work and need to switch"
│ ├─ Will return in < 5 min? → Use: git stash
│ └─ Will return later? → Use: git worktree
│
└─ "I'm not sure"
└─ Worktrees are safe and reversible - create one!
✅ Do:
repo-name-branch-namenpm install in each new worktreegit worktree list to see what's active❌ Don't:
npm install in new worktreeswt Script vs Raw Git Commandswt?The wt script handles:
../repo-name-branch)Without wt, you'd do this manually:
# Get repo name and default branch
repo_root=$(git rev-parse --show-toplevel)
repo_name=$(basename "$repo_root")
default_branch=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
# Create worktree
git worktree add -b DEV-123 ../${repo_name}-DEV-123 origin/$default_branch
# cd to it
cd ../${repo_name}-DEV-123
# Install dependencies
bun install # or npm/yarn/pnpm
With wt, you just do:
wt create DEV-123
wt scriptQuick start: Run wt list to see your worktrees. See INSTALL.md for setup.
For learning: This guide covers concepts, best practices, troubleshooting, and decision trees that help you understand when and why to use worktrees.
