Worktrees: many desks, one repo
Work on two branches at the same time — without stashing or switching.
1 Learn the idea
You are halfway through a feature, files everywhere, nothing ready to commit. Then: "Production is down, can you fix it now?" Your options so far were git stash + git switch, or a quick messy commit. Worktrees give you a third, calmer option.
How it works
git worktree add ../hotfix -b hotfix maincreates a new folder../hotfixwith a new branchhotfixchecked out, starting frommain.- Every worktree has its own working directory, staging area and HEAD — but they all share the same commits and branches.
- A commit made in one worktree is instantly visible from the others (it is the same repository).
- A branch can be checked out in only one worktree at a time — git refuses otherwise, so two desks never fight over the same branch.
2 Watch it happen
Press Play or use → to step through. Watch the graph as each command runs.
Read this demo as text
$ git status
We are on
featurewith uncommitted changes insearch.js. Then the call comes: production is crashing!$ git worktree add -b hotfix ../hotfix main
One command: a new folder
~/hotfix, a new branchhotfixstarting atmain, checked out there. Notice theworktree: hotfixlabel in the graph.$ cd ../hotfix
We walk to the second desk. It is clean and on
hotfix. Our unfinishedsearch.jswork is still sitting safely on the first desk.$ edit app.js "app without crash bug" $ git commit -am "Fix crash in production"
Fix and commit, as usual. The commit lands in the shared repository — the graph shows it on
hotfix.$ git worktree list
git worktree listshows every desk, which commit it is on, and which branch.$ cd ~/project
Back to the first desk…
$ git status
…and our half-done work is exactly as we left it. No stash, no WIP commit, no lost focus.
$ git switch hotfix
Trying to check out
hotfixhere too? Git refuses: that branch is already in use by the~/hotfixworktree. One branch, one desk.$ git worktree remove ../hotfix
Done with the hotfix? Remove the worktree. The folder disappears, but the branch and its commit stay — you can still merge or push
hotfix.$ git branch
hotfixis still listed. Worktrees are just desks; the work lives in the repository.
- One folder, one branch at a time
- Must pack up your changes first
- Easy to forget what is in the stash
- One folder per branch, side by side
- Your changes never move
- Costs almost no disk space: history is shared
git worktree add -b <new> <path> [<start>]New folder with a new branch checked outgit worktree add <path> <branch>New folder for an existing branchgit worktree listShow all worktrees and their branchesgit worktree remove <path>Delete the folder (the branch stays)3 Check your understanding
What do all worktrees of one repository share?
This repo has a worktree at ~/review. You are in ~/project and run git switch pr-42. What happens?
You run git worktree remove ../hotfix. What happens to the hotfix branch and its commits?
Which situation is the best fit for a worktree?
4 Practice for real
Fix production without packing up
You are mid-way through dark mode on feature, with an uncommitted theme.css. Without committing or stashing that work: create a worktree at ../hotfix on a new branch hotfix starting from main, commit a fix there, then return to ~/project.