Check out two branches at once with git worktree
How do I work on two branches at the same time without stashing or re-cloning?
If you’re seeing this error
fatal: '<branch>' is already checked out at '<path>'fatal: '<path>' is not a working treeYou’re in the right place — the fix is below.
Short answer
git worktree add ../hotfix main gives you a second folder checked out to a different branch, sharing the same .git object store. No stashing, no second clone, no waiting for your build to reinstall. When you're done, git worktree remove ../hotfix cleans it up.
git worktree add ../myrepo-hotfix main
cd ../myrepo-hotfix
A second working directory on a different branch, in one command
Does this match your situation?
- You need to review or hotfix another branch without disturbing half-finished work.
- 'fatal: '<branch>' is already checked out at '<path>''
- You keep stashing, switching, unstashing and losing your place.
- Switching branches forces a full rebuild or a dependency reinstall every time.
Step-by-step fix
Add a second working directory
One command creates a new folder checked out to the branch you name. It is a full working tree — you can build, test, commit and push from it exactly as normal.
step 1git worktree add ../myrepo-hotfix main # create a brand-new branch in the new folder: git worktree add -b hotfix/login ../myrepo-hotfix origin/mainSee what you have
Worktrees are easy to forget about. list shows every one, its path, its commit and its branch — including any left behind on a drive you have since unplugged.
step 2git worktree listUnderstand the one-branch-per-worktree rule
Git refuses to check the same branch out twice, because two folders committing to one branch would fight over the same ref. The error names the folder that already has it, which is usually all you need.
step 3git worktree list # find who holds the branch # work on it there, or make a new branch instead: git worktree add -b hotfix-2 ../another main--force overrides the check. Do it only if you understand that both checkouts will be moving the same branch pointer.
Inspect a detached commit without touching your branch
A detached worktree is the cleanest way to look at an old tag or a colleague's commit: nothing about your current branch or working directory changes.
step 4git worktree add --detach ../inspect v1.4.0Remove it when you're finished
remove deletes the folder and its administrative files. Deleting the folder by hand instead leaves a stale entry, which prune clears.
step 5git worktree remove ../myrepo-hotfix # if you already deleted the folder manually: git worktree prune
Why this works
A clone's real content lives in .git — the object database plus the refs — and the files you edit are just one checkout of it. A worktree separates those two ideas: it creates another checkout backed by the same object database, holding only a small file pointing back at the original .git. That is why adding one is nearly instant even on a huge repository and why it costs almost no extra disk, unlike a second clone which duplicates every object and starts with its own detached history, its own remotes and its own stale reflog. The single restriction — one branch checked out at a time — exists because a branch is a single pointer, and two working trees advancing it independently would silently lose commits.
If that didn’t work
- 'is not a working tree' usually means the folder was deleted by hand: run git worktree prune.
- A worktree on an unplugged external drive blocks removal — git worktree prune clears the record.
- Locked worktrees need git worktree unlock <path> before they can be removed.
- Submodules are not shared between worktrees; run git submodule update --init inside the new one.
How to stop it happening again
- Keep worktrees as siblings of the main folder (../repo-hotfix) so they never end up committed by accident.
- Run git worktree list before you go hunting for a branch Git says is already checked out.
- Use git worktree remove rather than deleting folders, so no stale records accumulate.
- Prefer a worktree over a second clone — the reflog, remotes and objects stay in one place.
Commands used in this guide
git worktreeCheck out several branches into separate folders from one repository.
git switchMove to another branch - the modern, safer half of git checkout.
git branchList, create, rename and delete branches.
git stashPark uncommitted work temporarily so you can do something else.
git cloneCopy a remote repository, its full history and its branches, onto your machine.
Still stuck?
Search the full command reference and every other rescue guide — there are 76 of them, covering everything from detached HEAD to force-push disasters.
Browse all guides →Frequently asked questions
What does 'fatal: branch is already checked out at' mean?
Another worktree has that branch checked out, and Git allows a branch in only one working tree at a time so two folders cannot advance the same pointer independently. Run git worktree list to see which folder holds it, and either work there or create a different branch.
Is a worktree better than cloning the repository twice?
Usually, yes. A worktree shares the object database, so it is near-instant to create, costs little disk, and gives you one reflog, one set of remotes and one place where every commit lands. A second clone duplicates everything and its history drifts out of step with the original.
Do worktrees share stashes and branches?
They share branches, tags, remotes, objects and the stash, because all of those live in the shared .git. What each worktree keeps to itself is its own HEAD, index and working files — which is exactly what lets you have two different branches checked out at once.
How do I delete a Git worktree?
git worktree remove <path>, which deletes the folder and its bookkeeping. If you already deleted the folder by hand, git worktree prune clears the stale record. Use --force when the worktree has uncommitted changes you are willing to lose.
Do worktrees work with submodules and hooks?
Hooks are shared, since core.hooksPath and .git/hooks live in the common directory — but a per-worktree config set with --worktree is not. Submodules are not automatically populated, so run git submodule update --init inside each new worktree.
Related rescue guides
Git says I'm in 'detached HEAD' — what does that mean and how do I fix it?
Detached HEAD means you're on a commit rather than a branch. If you made no commits, git switch - takes you back. If you did commit, create a branch at your current position first with git switch -c <name>, otherwise those commits have no reference and become hard to find.
Read the fix →Always fixablegit stash pop hit a conflict — how do I resolve it, and can I undo it?
Good news first: a conflicted pop never drops the stash, so your work is still safe in the stash list. Either resolve the markers and then git stash drop manually, or back out entirely with git checkout . followed by a fresh git stash apply once the tree is clean.
Read the fix →Always fixableI committed to main instead of my feature branch — how do I move the commits?
Create a branch at your current position to keep the commits, then reset the branch you polluted back to where it should be. Nothing is lost because the new branch holds the commits the whole time.
Read the fix →Always fixableCloning takes forever — how do I clone a big repo faster?
Use --filter=blob:none for a partial clone that fetches file contents lazily, and add sparse-checkout to materialise only the directories you need. --depth 1 is fastest of all but gives you no usable history.
Read the fix →