Using Git Worktrees
Git worktrees let you check out multiple branches simultaneously in different directories. Instead of constantly switching branches and stashing changes, you can work on multiple features, bug fixes, or experiments at the same time, each in its own isolated directory.
Why Use Worktrees?
- Work on multiple branches without switching context
- Test changes across different branches simultaneously
- Review PRs while keeping your current work untouched
- Avoid the git stash dance when you need to quickly switch tasks
Creating a Worktree
Let’s say you’re working on a feature but need to quickly fix a bug on main:
# Create a new worktree for the bugfix
> git worktree add ../project-bugfix main
# This creates a new directory with main checked out
> cd ../project-bugfix
> git checkout -b bugfix/urgent-fix
# Make your changes and commitListing Your Worktrees
You can see all your active worktrees and which branches they have checked out:
> git worktree list
/Users/dev/project        a1d2422 [feature/new-api]
/Users/dev/project-bugfix b3e4567 [bugfix/urgent-fix]
/Users/dev/project-review c5f6789 [main]Removing a Worktree
Once you’re done with a worktree, clean it up:
# Remove the directory first
> rm -rf ../project-bugfix
# Then prune the worktree from git's tracking
> git worktree prune
# Or use the remove command to do both
> git worktree remove ../project-bugfixQuick Tips
- Each worktree shares the same repository, so fetches and configuration changes apply to all
- You can’t check out the same branch in multiple worktrees
- Great for running tests on one branch while developing in another
- Use relative paths like ../project-nameto keep worktrees organized
Read more about it here