git worktree
Check out several branches at once in separate folders
The 'git worktree' command gives one repository several working folders. Each folder has a different branch checked out, with its own files and staging area, while the history is shared. It is the alternative to stashing when you must switch tasks: open the urgent branch in another folder and keep your work in progress untouched. A branch can be checked out in only one folder at a time.
Synopsis
git worktree add [-b <new-branch>] <path> [<commit-ish>]
git worktree list
git worktree remove <worktree>
git worktree pruneOptions
Choosing the Right Option
Switching tasks
Three ways
| Command | Your edits | Folders |
|---|---|---|
git stash + switch | Put aside, then restored | One |
git commit (WIP) + switch | In a temporary commit | One |
git worktree add | Untouched | Two |
Examples
New branch in a new folder
git worktree add ../hotfix -b hotfixFolder ../hotfix on a new branch.
Existing branch
git worktree add ../review feature/loginReview a branch without leaving yours.
List
git worktree listEvery folder with its branch.
Remove
git worktree remove ../hotfixDelete the extra folder.
Workflow Examples
git worktree add ../hotfix -b hotfix mainOpen main in a new folder
cd ../hotfixFix, commit, push there
git worktree remove ../hotfixClean up when done
Troubleshooting
fatal: 'hotfix' is already used by worktree
Cause: That branch is checked out in another folder
Work in that folder, or remove the worktree first
Tips & Pitfalls
Best Practices
- ✓Put worktrees next to the main folder (../name), not inside it.
Common Mistakes
- ✗Deleting a worktree folder by hand — use git worktree remove (or prune afterwards).
- ✗Trying to check out the same branch in two folders.
FAQ
Yes: one repository, one history; only the checked-out files differ.