Problem
Switching branches interrupts your workflow. You lose editor state, need to reinstall dependencies, and can’t easily compare implementations side-by-side. Working on multiple features or reviewing PRs requires constant context switching.Solution
Git worktrees create separate working directories for different branches, all sharing the same repository. Each worktree has its own files, node_modules, and environment—letting you work on multiple branches simultaneously.Implementation
Setup Script
Create a setup script that initializes worktrees with dependencies and environment files:scripts/worktree-setup.sh
IDE Integration (Cursor)
For Cursor IDE, add automatic dependency installation when opening worktrees:.cursor/worktrees.json
Git Aliases
Add these to your~/.gitconfig for faster worktree commands:
~/.gitconfig
Usage
Create a New Worktree
../chatjs-feature-auth/ with:
- Fresh checkout of the branch
- Copied
.env.localfrom main repo - Installed dependencies
Setup Existing Worktree
List Worktrees
Remove Worktree
Commands Reference
| Command | Description |
|---|---|
git wta <branch> | Add worktree for branch |
git wtl | List all worktrees |
git wtr <path> | Remove worktree |
git wtp | Prune stale worktrees |
git wt-cleanup | Remove merged branches and their worktrees |