Git Worktrees: Work on Several Branches at Once
Switching branches means stashing work, waiting for rebuilds and losing your place. A worktree is an extra working directory attached to the same repository, so different branches can be checked out at the same time.
Basic commands
# Check out an existing branch in a sibling folder
git worktree add ../shop-review feature/coupons
# Create a new branch in a new worktree
git worktree add -b hotfix/vat-rounding ../shop-hotfix main
git worktree list
git worktree remove ../shop-review
git worktree prune # clean up references to deleted folders
All worktrees share one .git object store, so they're cheap: no second clone, and commits made in one are immediately visible to the others.
Great uses
- Review a pull request while your own work stays untouched.
- Urgent hotfix without stashing a half-finished feature.
- Long-running tasks: run a slow test suite or build in one worktree while you keep coding in another.
- Comparing behaviour between two versions side by side.
- Parallel AI coding agents: give each agent its own worktree and branch, so they can edit files at the same time without stepping on each other.
Things to know
- The same branch can't be checked out in two worktrees at once.
- Each worktree needs its own dependencies (
node_modules, virtual environments) and build output. - Untracked files like
.envaren't copied; set them up per worktree. - Delete worktrees with
git worktree removerather thanrm -rf, or rungit worktree pruneafterwards.
Was this article helpful?
Comments
No comments yet. Have a question or a tip? Be the first to share it.