Git Worktrees
Git worktrees allow you to check out multiple branches simultaneously in different directories. This is useful when you need to work on multiple features or bug fixes without constantly switching branches or stashing changes.
Creating a New Worktree
Create a new worktree in a separate directory:
# Create a new worktree for an existing branchgit worktree add ../feature-branch feature-branch# Create a new worktree and new branch in one commandgit worktree add -b new-feature ../new-feature# Create from a specific remote branchgit worktree add ../bugfix origin/bugfix
Listing Worktrees
See all worktrees associated with your repository:
git worktree list# Output example:# /path/to/main-repo abcd123 [master]# /path/to/feature-branch ef45678 [feature-branch]
Removing a Worktree
Remove a worktree when you're done with it:
# Remove the worktree directory firstrm -rf ../feature-branch# Then remove the worktree from git's trackinggit worktree remove ../feature-branch# Or force removal even with uncommitted changesgit worktree remove --force ../feature-branch
Cleaning Up Stale Worktrees
Remove worktree administrative data for directories that have been manually deleted:
git worktree prune# See what would be pruned without actually doing itgit worktree prune --dry-run
Common Use Cases
- Emergency hotfixes: Quickly switch to production code without stashing your current work
- Code reviews: Check out a PR branch while keeping your main work intact
- Testing: Run tests on one branch while developing on another
- Comparing branches: Have multiple branches open side-by-side for easy comparison