You're forty minutes into a refactor, nothing compiles yet, and someone needs a one-line fix on main shipped now. The usual dance is git stash, git switch main, fix, push, git switch -, git stash pop, and then a merge conflict in the stash because the fix touched the same file. Or you commit "wip" and promise yourself you'll clean it up.
There's a third option that most people only find after years of the dance: check main out in a second folder and leave the refactor exactly where it is.
Short answer:
git worktree add ../myapp-hotfix -b hotfix maincreates a new folder withmainchecked out on a freshhotfixbranch, linked to the same repository. Fix, commit and push from there, thengit worktree remove ../myapp-hotfix. Your original folder, its uncommitted changes and its running dev server are never touched. One rule to remember: a branch can only be checked out in one worktree at a time.
The five commands you'll actually use
Every example below ran against Git 2.43 in a throwaway repo, so the output is real.
# New folder, new branch "hotfix" starting from main
git worktree add ../app-hotfix -b hotfix main
# New folder for an existing branch
git worktree add ../app-feature feature
# New folder at a commit, no branch at all (good for "let me just look at this")
git worktree add --detach ../app-review v2.3.1
# What do I have?
git worktree list
# Done with it
git worktree remove ../app-hotfixgit worktree list shows every checkout, its commit and its branch:
$ git worktree list
/home/me/src/app 01c0aca [main]
/home/me/src/app-feature bc7a1be [feature]
/home/me/src/app-hotfix 01c0aca [hotfix]Put the worktrees next to the main checkout (../app-hotfix), not inside it. A worktree inside the repo shows up as an untracked directory in git status unless you ignore it, and your editor's file search will happily index every copy. If a tool insists on nesting them (Claude Code puts its own under .claude/worktrees/), add that path to .gitignore (the gitignore generator will give you a sane base file to add it to).
What a worktree actually is
A linked worktree isn't a clone. Open the .git in one and it's a one-line file, not a directory:
$ cat ../app-feature/.git
gitdir: /home/me/src/app/.git/worktrees/app-featureAll the objects, branches and remotes live once, in the main repository's .git. Each worktree gets a small private folder under .git/worktrees/<name>/ holding the things that have to be different per checkout. That split is the whole mental model, and it explains every surprise below:
| Thing | Shared across worktrees? | What that means in practice |
|---|---|---|
| Commits, objects | Shared | A commit made in one worktree is instantly visible in the others. No fetch, no push. |
| Branches and tags | Shared | git branch lists the same branches everywhere. |
Remotes and git fetch | Shared | Fetch once in any worktree and all of them see the new origin/* refs. |
| Stash | Shared | git stash list shows the same list in every worktree. Easy to pop someone else's stash. |
Config (.git/config) | Shared | Unless you turn on extensions.worktreeConfig (see below). |
| Hooks | Shared | Your pre-commit hook runs in every worktree. |
HEAD and the index | Per worktree | Each has its own checked-out branch and its own staging area. |
| Bisect, rebase in progress | Per worktree | You can run git bisect in one folder while working in another. |
| Working files | Per worktree | Including everything gitignored: node_modules, .env, build output. |
That last row is the one that bites. A new worktree is a clean checkout, so it has no node_modules, no .env, no local build cache. Plan on an install step every time, and copy the env file across by hand (or script it).
The stash row is the sneaky one. I tested it: stashing in app-feature and then running git stash list from the main checkout shows stash@{0}: WIP on feature: bc7a1be add parser. If you share a machine with an AI agent that stashes, or you just forget which folder you stashed from, git stash pop in the wrong worktree applies someone else's half-finished work to your branch. Use named stashes (git stash push -m "...") if you mix stashing and worktrees. Better yet, stop needing the stash; that was the point.
Where it earns its keep
Hotfix without touching your work in progress
The opening scenario. Your refactor folder keeps its dirty files, its open editor tabs and its dev server on port 3000. The hotfix folder gets its own install and its own port. When the fix is merged, git worktree remove it and you never left your refactor.
Reviewing a PR you need to actually run
git fetch origin
git worktree add ../app-pr-482 origin/fix-login-redirectWhen the branch only exists on the remote, Git does the sensible thing and creates a local tracking branch for you:
Preparing worktree (new branch 'fix-login-redirect')
branch 'fix-login-redirect' set up to track 'origin/fix-login-redirect'.Run it, poke it, leave comments, delete the folder. If you only want to read code without a branch, --detach skips creating one.
Long builds and test runs
Start the slow integration suite in one worktree and keep editing in the other. With a single checkout you either wait or risk the tests picking up half-saved files.
Parallel AI coding agents
This is the use case that made worktrees mainstream in 2025 and 2026. Two agents editing the same folder will overwrite each other's files and fight over git add. Give each one its own worktree and branch and they can't. Claude Code's --worktree flag does exactly this: claude --worktree feature-auth creates a worktree under .claude/worktrees/feature-auth/ on a new worktree-feature-auth branch, and other agent tools have their own equivalent. The same caveats apply as for humans: each worktree needs its own install and its own dev-server port, and gitignored files like .env don't come along unless something copies them.
The errors you'll hit, and what they mean
"fatal: 'feature' is already used by worktree at ..."
$ git worktree add ../app-feature2 feature
fatal: 'feature' is already used by worktree at '/home/me/src/app-feature'The one rule: a branch can be checked out in only one worktree, because two checkouts moving the same branch tip would silently clobber each other. You get the same error from a plain git checkout feature in another worktree. The fix is almost always to go to the folder that already has it (git worktree list tells you where), or to make a new branch from it: git worktree add ../app-feature2 -b feature-experiment feature. There are override flags (--force on add, --ignore-other-worktrees on checkout). Don't use them unless you can say exactly why.
The same protection covers deleting and moving branches:
$ git branch -D hotfix
error: cannot delete branch 'hotfix' used by worktree at '/home/me/src/app-hotfix'
$ git branch -f hotfix main
fatal: cannot force update the branch 'hotfix' used by worktree at '/home/me/src/app-hotfix'That second one catches people who try to fast-forward main from a feature worktree while main is checked out in the original folder. Pull from the folder that has main, or don't keep main checked out anywhere (a detached main checkout is a legitimate setup).
"contains modified or untracked files, use --force to delete it"
$ git worktree remove ../app-feature
fatal: '../app-feature' contains modified or untracked files, use --force to delete itGit won't throw away uncommitted work for you. Commit or move what matters, then remove. --force deletes the folder and the uncommitted changes in it, which, unlike committed work, the reflog can't bring back. Note that remove never deletes the branch, so committed work on it is safe either way.
A worktree marked "prunable"
If you delete a worktree folder with rm -rf instead of git worktree remove, the bookkeeping in .git/worktrees/ stays behind and the branch still counts as "checked out":
$ git worktree list
/home/me/src/app 01c0aca [main]
/home/me/src/app-review 01c0aca (detached HEAD) prunable
$ git worktree prune -v
Removing worktrees/app-review: gitdir file points to non-existent locationgit worktree prune cleans up those stale entries. Git also prunes them on its own during garbage collection after a grace period, but don't wait for that if a branch is stuck as "already used".
You moved the folder and now Git is confused
Moving a worktree with mv breaks the absolute path stored in both directions. git worktree list flags the old path as prunable, and the moved folder stops working as a repo. Either move it the supported way, git worktree move ../app-hotfix ../hotfix, or repair after the fact by running git worktree repair <new-path> from the main checkout:
$ git worktree repair /home/me/src/moved
repair: gitdir incorrect: /home/me/src/app/.git/worktrees/app-hotfix/gitdirIf you move repos around a lot (or mount them into containers at different paths), Git 2.48 added worktree.useRelativePaths, which stores the links as relative paths. It turns on a repository extension that older Git versions refuse to read, so check that every Git that touches the repo (CI images, IDE-bundled Git, containers) is 2.48 or newer first.
If a worktree lives somewhere that isn't always there (a USB drive, a network share), run
git worktree lock <path> --reason "on USB". Prune skips locked worktrees, so the bookkeeping survives while the drive is unplugged.
Per-worktree config, when you need it
Config is shared by default, which is what you want 95% of the time. When it isn't (a different user.email for one checkout, or sparse checkout in only one), turn on the extension once and use --worktree:
git config extensions.worktreeConfig true
git config --worktree core.sparseCheckout trueValues set with --worktree live in that worktree's private config.worktree and override the shared file. Like relative paths, this extension makes older Git versions refuse the repository, so it's a team decision, not a personal one.
The bare-repo layout, and its one trap
A popular setup is to keep no "main" checkout at all: clone bare, then make a worktree per branch.
git clone --bare git@github.com:you/app.git app/.bare
echo "gitdir: ./.bare" > app/.git
cd app
git worktree add main
git worktree add feature-xEvery branch becomes a sibling folder and nothing is special. The trap: git clone --bare doesn't write a fetch refspec, so git fetch updates nothing under origin/* and new remote branches never appear. I checked: git config --get remote.origin.fetch on a fresh bare clone prints nothing. Add it yourself:
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetchIt's a nice layout if you live in many branches at once. For the occasional hotfix, plain git worktree add from your normal clone is less ceremony.
When it isn't worth it
Worktrees cost disk (a full working tree each, plus a full node_modules in JS projects) and setup time per folder. If your project takes ten minutes to install and build from clean, a worktree for a two-line fix may be slower than stashing. And they don't help when the real problem is two changes that should be one branch. For the everyday context switch, though, they replace the stash dance with something that can't conflict, can't get lost, and doesn't make you remember what you were doing.
If you still reach for the stash for quick, same-branch shelving, the stash has more to it than push and pop. Just keep it for that, and give each branch its own folder.
Cover photo by Max Vakhtbovych on Pexels.
