You're halfway through a feature. Someone asks you to review a PR, or production needs a hotfix. The usual dance is git stash, switch, do the thing, switch back, git stash pop, and hope nothing conflicts.
git worktree skips all of that. It checks out another branch into a separate folder, backed by the same repository.
Add a worktree
# from inside your repo git worktree add ../myapp-hotfix -b hotfix/login main
That creates ../myapp-hotfix, makes a new branch hotfix/login from main, and checks it out there. Your original folder is untouched, uncommitted changes and all.
To check out an existing branch:
git worktree add ../myapp-review feature/new-checkout
Work in it
cd ../myapp-hotfix # edit, test, commit, push, as normal git commit -am "Fix login redirect" git push -u origin hotfix/login
Commits are written to the shared repository, so you can git log hotfix/login from the original folder straight away.
List and remove
git worktree list # /code/myapp a1b2c3d [feature/payments] # /code/myapp-hotfix e4f5a6b [hotfix/login] git worktree remove ../myapp-hotfix
If you deleted the folder by hand instead, clean up Git's records:
git worktree prune
The branch survives removing the worktree. Delete it separately with git branch -d hotfix/login if you're done.
Where worktrees shine
Reviewing a PR without losing your place. Check out the PR branch in its own folder, run it, leave comments, remove it.
Hotfixes mid-feature. Your feature branch keeps its half-finished state, dev server and all.
Comparing behaviour. Run main on port 3000 and your branch on 3001, side by side.
Long builds or test runs. Kick off a slow test suite in one worktree while you keep coding in another.
Running AI coding agents in parallel. Give each agent its own worktree and branch. They can each edit files, run tests, and commit without stepping on each other or on your working copy. When one finishes, review its branch like any PR. Several AI coding tools now create worktrees for this automatically.
Things to know
- One branch, one worktree. Git won't check out the same branch in two places. Use
--detachif you just want to look at a commit. - Dependencies aren't shared. Each folder needs its own
node_modules(pnpm's shared store makes this cheap) and its own.envfile, since ignored files aren't copied. - Hooks and config are shared, because they live in the common
.gitdirectory. - Keep worktrees outside the main folder (
../name), or add their folder to.gitignore, so tools don't index them twice.
A naming convention that helps
git worktree add ../$(basename "$PWD")-review -b review/pr-142 origin/pr-branch
A Git alias shortens it further:
git config --global alias.wt "worktree" git wt add ../app-fix -b fix/thing
Worktree vs stash vs clone
| Stash | Worktree | Second clone | |
|---|---|---|---|
| Keeps current changes in place | No | Yes | Yes |
| Disk use | None | Working files only | Everything |
| Shares commits instantly | Yes | Yes | No, must fetch |
| Good for | quick 2-minute switches | parallel work | totally separate setups |