You are halfway through a feature, the build is warm, and a hotfix lands on your desk. The usual move is to stash your work, switch branches, fix, switch back, and pop the stash — throwing away your build cache twice and hoping nothing was lost. git worktree removes that dance. It gives one clone several working directories, each with a different branch checked out, all sharing the same object database. Your feature branch stays exactly as you left it in ~/proj while the hotfix gets its own directory in ~/hotfix. This guide covers git worktree add in its useful forms, list, remove, prune, and repair, plus the failure modes that catch people the first week.
What you’ll accomplish
- Check out a second (and third) branch into its own directory without touching your current work.
- Create a worktree on a brand-new branch, or on a detached HEAD for throwaway experiments.
- Read
git worktree listand know which branch lives where and which entries are stale. - Clean up correctly with
git worktree remove, and repair the mess when you delete a directory by hand. - Know when a plain
git switchis the better answer.
Prerequisites
- Git 2.5 or newer (
git --version). The core ofgit worktreehas been stable since 2.5, so anything from the last decade will do foradd,list, andprune. - Git 2.17+ for
moveandremove, which arrived later. Before that, cleaning up meant deleting the directory and runningpruneyourself. - Git 2.30+ for
git worktree repair, and 2.42+ forgit worktree add --orphan. These two are the newest pieces here — if you are on a distro-packaged Git from an older LTS, check before relying on them. - A normal (non-bare) clone. Worktrees work from bare repositories too, but the layout below assumes an ordinary clone.
- Enough disk for a second checkout of your tree. History is shared; the working files are not.
How it works
A clone has two things that look like one: the object database (every commit, tree, blob, ref, and hook — the contents of .git/) and the working directory (the files you edit). A normal clone pins exactly one working directory to that database. git worktree breaks the 1:1 assumption and lets several working directories attach to the same store.
The main worktree keeps a real .git directory. Each linked worktree instead gets a .git file — one line of text pointing back at a per-tree metadata directory inside the main repo:
cat ~/hotfix/.git
gitdir: /home/dev/proj/.git/worktrees/hotfix
That is the entire trick. Commits, tags, and fetched objects are written once and visible from every tree instantly — commit in ~/hotfix and the SHA is immediately reachable from ~/proj. What is not shared is per-tree state: HEAD, the index, the stash, and anything untracked or ignored (your node_modules, your target/, your build cache). That separation is the whole point — it is why a long test run in one tree cannot disturb an edit in another.
Git enforces one rule to keep this coherent: the same branch cannot be checked out in two worktrees at once. Two trees advancing one branch would corrupt each other’s idea of HEAD, so Git refuses.
Step-by-step
1. Check where you are. Every worktree command is run from inside any tree of the repo. git worktree list works from day one — a fresh clone is already a worktree, the main one.
git worktree list
/home/dev/proj 84df2e2 [feature/checkout]
2. Add a worktree for an existing branch. Give add a path and a branch. The path is relative to your current directory, so ../hotfix puts it beside the repo rather than inside it. Git creates the directory, checks the branch out there, and leaves your current tree untouched.
git worktree add ../hotfix main
Preparing worktree (checking out 'main')
HEAD is now at 5f67dc0 Cache invalidation on write
3. Add a worktree on a new branch. Most hotfix work starts from a fresh branch. -b creates the branch and checks it out in the new tree in one step. The final argument is the start point — where the branch begins.
git worktree add -b hotfix/login ../hotfix-login main
Preparing worktree (new branch 'hotfix/login')
HEAD is now at 5f67dc0 Cache invalidation on write
4. Add a throwaway tree with a detached HEAD. When you only want to look at a commit — review a tag, run a bisect, check what a release actually shipped — you do not want a branch at all. --detach checks out the commit with no branch attached, so nothing you do there claims a branch name.
git worktree add --detach ../scratch v2.3.0
Preparing worktree (detached HEAD 5f67dc0)
HEAD is now at 5f67dc0 Cache invalidation on write
This is the tidiest way to run a git bisect session: bisect leaves you on a detached checkout and refuses to move over uncommitted edits, so doing it in a scratch tree keeps your real working directory out of the fight entirely.
5. Inspect what you now have. list is the map: path, HEAD, and branch for every tree.
git worktree list
/home/dev/proj 84df2e2 [feature/checkout]
/home/dev/hotfix 5f67dc0 [main]
/home/dev/hotfix-login 5f67dc0 [hotfix/login]
/home/dev/scratch 5f67dc0 (detached HEAD)
For scripts, --porcelain gives a stable, parseable form instead of aligned columns.
git worktree list --porcelain
worktree /home/dev/proj
HEAD 84df2e216e2344d4dbf8f4b85f8136ec6093211e
branch refs/heads/feature/checkout
worktree /home/dev/scratch
HEAD 5f67dc0c51e38021f32a8ced6ea5c7064aa7b6f0
detached
6. Work in the new tree. Nothing special here — it is an ordinary checkout. cd into it and use Git normally. Commits you make are visible from every other tree the moment they exist.
cd ../hotfix-login
git commit -am "Reject empty password before hashing"
git push -u origin hotfix/login
[hotfix/login 3a91c4e] Reject empty password before hashing
1 file changed, 4 insertions(+)
7. Remove the tree when the work lands. remove deletes the directory and the metadata entry — this is the correct cleanup, and it prints nothing on success.
git worktree remove ../hotfix-login
Note that removing a worktree does not delete the branch; hotfix/login still exists and still has your commits. It only tears down the checkout.
Validate it works
The claim worth testing is that the object store is genuinely shared. Commit in one tree and look for the SHA from another:
cd ~/hotfix-login && git commit -am "Reject empty password" --allow-empty
cd ~/proj && git log --oneline -1 hotfix/login
3a91c4e Reject empty password
The commit made in ~/hotfix-login is readable from ~/proj with no fetch, no push, no sync — one database. Then confirm the isolation half by checking that each tree kept its own branch:
git worktree list
/home/dev/proj 84df2e2 [feature/checkout]
/home/dev/hotfix-login 3a91c4e [hotfix/login]
Your feature/checkout work is still sitting at 84df2e2, untouched, with its build directory intact.
Troubleshooting
“fatal: ‘main’ is already used by worktree at …”. This is the one-branch-one-tree rule. You already have main checked out somewhere — often the main clone you forgot you were sitting on.
git worktree add ../release main
Preparing worktree (checking out 'main')
fatal: 'main' is already used by worktree at '/home/dev/hotfix'
Run git worktree list to find the tree holding it. Then either work in that tree, branch off instead (git worktree add -b release-prep ../release main), or use --detach if you only need to read the code. --force will override the check, but you are then on your own to keep the two trees from clobbering each other — that is rarely what you want.
A tree shows as prunable after you deleted the directory by hand. rm -rf ../scratch removes the files but not the entry under .git/worktrees. Git notices the directory is gone and flags it:
/home/dev/scratch 5f67dc0 (detached HEAD) prunable
Clear the stale entries with prune. Use -n first to see what would go, and -v to have it narrate:
git worktree prune -v
Removing worktrees/scratch: gitdir file points to non-existent location
Nothing is lost — prune only discards bookkeeping. Prefer git worktree remove next time and you skip this step.
Every Git command in a linked tree fails after you moved the main repo. Those gitdir: pointers are absolute paths. Move or rename the parent repo and the linked trees are orphaned:
fatal: not a git repository: /home/dev/proj/.git/worktrees/hotfix
Run git worktree repair from the main repo to rewrite the pointers on both sides:
cd /home/dev/proj-moved
git worktree repair
repair: .git file broken: /home/dev/hotfix
repair: .git file broken: /home/dev/docs
Despite reading like errors, those lines are the repair report — it is telling you what it fixed. Run git worktree list afterwards and the new paths are in place. If you moved a worktree rather than the main repo, pass its new path: git worktree repair /new/path/to/hotfix. To avoid the problem entirely, relocate trees with git worktree move ../hotfix ../hotfix-2 instead of mv.
Your new tree has no node_modules and the build starts from zero. Untracked and ignored files are not shared — that is by design, and it is also the cost. A new worktree needs its own npm install, its own .env, its own build output. Budget for it: three JavaScript worktrees means three node_modules. Copy the ignored files you need, symlink a shared cache directory, or accept the first build being cold.
Submodules do not come along. git worktree add does not populate submodules in the new tree, and submodule support in worktrees has rough edges. Run git submodule update --init --recursive inside the new tree after creating it. If your repo leans heavily on submodules, test the flow on a scratch tree before you commit to it as a habit.
Refusing to remove a tree with local changes. remove protects you from deleting unsaved work:
fatal: '../hotfix' contains modified or untracked files, use --force to delete it
Check what is in there before reaching for --force. Once you are sure the changes are disposable, git worktree remove --force ../hotfix tears it down.
When this is the wrong approach
Worktrees are a good tool with a narrow sweet spot, and the honest answer is that most branch switches do not need one.
- A plain
git switchis enough for most work. If your tree is clean and the build is cheap, switching branches takes a second and costs nothing. Worktrees earn their keep when the state is expensive to rebuild — a warm compile cache, a running dev server, a half-finished rebase, a populated test database. Reach for one when switching would destroy something you want to keep, not as your default. - Disk grows fast. Each tree is a full checkout plus its own build artifacts. On a large monorepo with a multi-gigabyte
target/ornode_modules, three worktrees is a real bill.git switchreuses one directory; worktrees multiply it. - Some tooling gets confused. Editors and IDEs that assume
.gitis a directory, or that a project has one root, can misbehave — indexers re-scan every tree, some Git integrations do not recognise the.gitfile pointer, and tools that write to.gitdirectly may pick the wrong one. Most modern tooling handles it; test yours before you restructure your week around it. - You just want to read one file from another branch.
git show other-branch:path/to/fileanswers that without any checkout at all. - You need genuinely independent history or remotes. Worktrees share one object store and one set of remotes. If you want isolation at that level — different remotes, different config, an experiment you might delete wholesale — a second clone is the simpler, more honest answer.
The authoritative reference is the official git worktree documentation. If a worktree is where you are chasing a regression, pair it with git bisect; if a cleanup went further than you meant it to, git reflog is usually the way back.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.