Working on Several Branches with Worktrees
Normally a repository has one working tree, so working on another branch means stashing or committing and switching. git worktree lets you check out additional branches into separate folders that all share the same repository — so you can fix a bug on one branch while a long build or half-finished work sits untouched on another.
The situation
Sam is halfway through the wishlist feature, with uncommitted edits, when an urgent fix is needed on main:
git status -s git branch
M wishlist.html * feature/wishlist main
Adding a worktree
Instead of stashing, Sam checks out a new branch into a sibling folder:
git worktree add -b fix/footer-typo ../shop-hotfix main
Preparing worktree (new branch 'fix/footer-typo') HEAD is now at 271266e Create home page
This creates the folder ../shop-hotfix with branch fix/footer-typo (started from main) checked out. The original folder, with its uncommitted wishlist edits, is untouched.
git worktree list
/home/sam/shop dd929ef [feature/wishlist] /home/sam/shop-hotfix 271266e [fix/footer-typo]
Working in it
The new folder is a full working tree; commits made there go into the same repository:
cd ../shop-hotfix echo "<footer>Contact us</footer>" >> index.html git commit -qam "Fix footer typo" git log --oneline -1
a59d734 (HEAD -> fix/footer-typo) Fix footer typo
Back in the main folder, the new branch and commit are immediately visible — there's only one repository, so no fetching or pushing between folders:
cd ../shop git log --oneline -1 fix/footer-typo git status -s
a59d734 (fix/footer-typo) Fix footer typo M wishlist.html
One branch, one worktree
A branch can be checked out in only one worktree at a time, so two folders can't move the same branch in different directions:
git switch fix/footer-typo
fatal: 'fix/footer-typo' is already checked out at '/home/sam/shop-hotfix'
Removing a worktree
When you're done, remove the folder through Git (the branch and its commits stay):
git worktree remove ../shop-hotfix git worktree list
/home/sam/shop dd929ef [feature/wishlist]
If you delete a worktree folder by hand instead, run git worktree prune to clean up Git's records of it.
Commands
| Command | Does |
|---|---|
git worktree add ../dir branch | Check out an existing branch in a new folder |
git worktree add -b new ../dir start | Create a branch from start and check it out there |
git worktree add --detach ../dir v1.0 | Check out a commit or tag without a branch |
git worktree list | Show all worktrees |
git worktree remove ../dir | Remove one (refuses if it has uncommitted changes, unless --force) |
git worktree prune | Forget worktrees whose folders were deleted |
When worktrees help
- Urgent fixes without disturbing work in progress.
- Reviewing a colleague's branch while your own build or tests keep running.
- Comparing two versions of an app side by side.
- Running several AI coding agents or long jobs in parallel, each on its own branch.
Each worktree needs its own installed dependencies and build output (for example its own node_modules), so they use more disk space than a single checkout — but far less than a second clone, because the history is shared.
Try it yourself
With uncommitted changes in your main folder, add a worktree for a new branch, commit something there, and confirm with git log --oneline --all from the main folder that the commit is visible. Then remove the worktree.