Worktrees: Several Branches Checked Out at Once
Work on several branches at the same time with git worktree: adding worktrees for a hotfix or a review, what's shared and what's separate, the one-branch-one-worktree rule, listing, locking, moving, removing and pruning, relative paths, and the bare-repository layout.
kvi5hnuCode Sprout team member6 min readOn this page
You're in the middle of a feature, with half-finished changes, when an urgent bug needs fixing on main. Stashing works (article 27), but switching branches throws away your editor state, triggers a rebuild, and is easy to get wrong. git worktree gives you another option: a second working folder for the same repository, with a different branch checked out. Both share one history, so commits made in either are immediately visible in the other, and nothing is cloned or downloaded.
Adding a worktree
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
git switch -q -c feature/maps
echo "unfinished map work" > maps.txt # uncommitted, and stays here
git worktree add -b hotfix/distance ../trail-guide-hotfix main
cd ../trail-guide-hotfix
echo "Lake loop: 5.4 km" > lake.txt
git commit -q -am "Fix lake distance"
cd ../trail-guide
git worktree list
echo "=== the hotfix commit is visible here at once"
git log --oneline hotfix/distance -1
git status --short # untouched
Preparing worktree (new branch 'hotfix/distance')
HEAD is now at 65a5265 Add lake page
/workspace/trail-guide 65a5265 [feature/maps]
/workspace/trail-guide-hotfix d9078c7 [hotfix/distance]
=== the hotfix commit is visible here at once
d9078c7 Fix lake distance
?? maps.txt
git worktree add <path> <branch>checks out an existing branch in a new folder.-b <new-branch>creates the branch first, starting from the commit given last, heremain.git worktree add <path>alone creates a branch named after the folder, fromHEAD.--detachchecks out a commit without a branch, and--trackwith a remote-tracking branch sets up the new branch's upstream.- Put worktrees next to the main folder, not inside it, so they don't appear as untracked files.
The original folder is the main worktree, and others are linked worktrees. Typical uses:
- an urgent fix, without disturbing your current work;
- reviewing a pull request, building and running it, while your own branch stays open in the editor;
- running a long test suite or build on one branch while you keep working on another;
- comparing two versions side by side, or running several tools or AI coding agents on separate branches in parallel.
One branch, one worktree
A branch can be checked out in only one worktree at a time. Otherwise a commit in one folder would move the branch under the other's feet:
git init -q trail-guide && cd trail-guide
git commit -q --allow-empty -m "Add lake page"
git branch review
git worktree add -q ../trail-guide-review review
git switch review; echo "exit status $?"
echo "=== + marks branches checked out in other worktrees"
git branch
fatal: 'review' is already used by worktree at '/workspace/trail-guide-review'
exit status 128
=== + marks branches checked out in other worktrees
* main
+ review
In git branch, * is the current branch and + marks branches checked out in another worktree, which git branch -d also refuses to delete. Detached HEADs have no such restriction, so several worktrees can look at the same commit.
What's shared and what's separate
All worktrees share the repository: commits and other objects, branches and tags, remotes, configuration, stashes, hooks, and .git/info/exclude. Each worktree has its own files on disk, HEAD, index (staging area), and per-operation state such as a rebase or merge in progress.
A linked worktree's .git is a file, not a folder, pointing to its private area inside the main repository:
git init -q trail-guide && cd trail-guide
git commit -q --allow-empty -m "Add lake page"
git worktree add -q -b review ../trail-guide-review
cat ../trail-guide-review/.git
git -C ../trail-guide-review rev-parse --git-dir --git-common-dir
echo "=== with relative paths (Git 2.48)"
git worktree add -q --relative-paths -b docs ../trail-guide-docs
cat ../trail-guide-docs/.git
gitdir: /workspace/trail-guide/.git/worktrees/trail-guide-review
/workspace/trail-guide/.git/worktrees/trail-guide-review
/workspace/trail-guide/.git
=== with relative paths (Git 2.48)
gitdir: ../trail-guide/.git/worktrees/trail-guide-docs
Worktrees normally record absolute paths, so moving the whole set of folders, or using them from a container that mounts them elsewhere, breaks the links. --relative-paths, or worktree.useRelativePaths=true (Git 2.48), records relative paths instead. git worktree repair fixes links after folders were moved by hand.
Settings that should differ per worktree, such as sparse checkout (article 44), go in the worktree configuration level: git config set extensions.worktreeConfig true, then git config set --worktree <key> <value> (article 33).
Listing, removing and cleaning up
git init -q trail-guide && cd trail-guide
git commit -q --allow-empty -m "Add lake page"
git worktree add -q -b review ../trail-guide-review
git worktree add -q -b experiment ../trail-guide-experiment
git worktree add -q -b old ../trail-guide-old
echo "draft" > ../trail-guide-review/notes.txt
git worktree remove ../trail-guide-review; echo "exit status $?" # refuses: untracked work
git worktree remove ../trail-guide-experiment # clean: removed
rm -r ../trail-guide-old # deleted by hand...
git worktree prune -v # ...so tidy the records
git worktree list
git branch # branches are kept
fatal: '../trail-guide-review' contains modified or untracked files, use --force to delete it
exit status 128
Removing worktrees/trail-guide-old: gitdir file points to non-existent location
/workspace/trail-guide d7b060a [main]
/workspace/trail-guide-review d7b060a [review]
experiment
* main
old
+ review
git worktree remove <path>deletes the folder and its records. It refuses if the worktree has modified or untracked files;--forceoverrides that.- Removing a worktree doesn't delete its branch. Delete it separately with
git branch -dwhen it's merged. - If a worktree folder is deleted by hand, Git keeps its records until
git worktree prune, which also runs during garbage collection. Until then, its branch counts as checked out. git worktree lock <path> --reason "..."protects a worktree on removable or network storage from being pruned or moved while it's unavailable.unlockreverses it.git worktree move <old> <new>moves a worktree, keeping its records correct.git worktree list --porcelaingives script-friendly output, including locked and prunable states.
A bare repository with worktrees
Some developers keep no "main" checkout at all: a bare clone holds the repository, and every branch they work on is a worktree beside it:
git clone --bare git@github.com:acme/trail-guide.git trail-guide/.bare
cd trail-guide
echo "gitdir: ./.bare" > .git
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" # bare clones don't set this
git fetch
git worktree add main
git worktree add -b feature/maps feature/maps origin/main
Each branch then has a sibling folder: trail-guide/main, trail-guide/feature/maps. The fetch refspec line matters, because a bare clone otherwise doesn't create remote-tracking branches.
What's current
- Git 2.48 added relative worktree paths (
--relative-paths,worktree.useRelativePaths), with a repository extension so older Git versions don't misread them. - Editors and IDEs increasingly support worktrees directly, and running several coding agents on separate worktrees has become a common reason to use them.
git branchmarks branches checked out in other worktrees with+, andgit statusand the prompt work normally inside each worktree.
Common questions
Worktree or a second clone? A worktree shares one repository: no extra download, little extra disk space, and commits and fetches are visible everywhere at once. A second clone is fully independent, which only helps if you want separate configuration or remotes.
Why does git switch say the branch "is already used by worktree"?
That branch is checked out in another worktree. Work on it there, or git worktree list to find the folder, and remove that worktree if you no longer need it.
I deleted a worktree folder, and now I can't check out its branch. Why?
Git still has the worktree's records. Run git worktree prune, and the branch is free again.
Do worktrees share stashes?
Yes. The stash is stored in the shared repository, so git stash list shows the same entries in every worktree.
Summary
git worktree add <path> <branch>, or-b <new> <path> <start>, checks out another branch in a separate folder of the same repository.- Worktrees share history, branches, configuration and stashes. Each has its own files,
HEADand index. - A branch can be checked out in only one worktree.
+ingit branchmarks branches used elsewhere. git worktree list,remove,prune,lock,moveandrepairmanage them. Removing a worktree keeps its branch.--relative-paths(Git 2.48) makes worktree links survive moving the folders.
Practice questions
You're mid-feature with uncommitted changes, and need to fix a bug on main and push it. How do you do it with a worktree?
Answer
git worktree add -b fix/bug ../project-fix main
cd ../project-fix
# fix, test, commit
git push -u origin fix/bug
cd ../project
git worktree remove ../project-fix
Your feature folder and its uncommitted changes are never touched. The fix/bug branch remains after removing the worktree, for its pull request.
git worktree add ../review feature/login fails with "'feature/login' is already used by worktree". What are your options?
Answer
The branch is checked out in another worktree. Use that worktree (find it with git worktree list), or check out the commit detached for read-only review: git worktree add --detach ../review feature/login. If the other worktree is stale or its folder was deleted, git worktree remove or git worktree prune frees the branch.
After git worktree remove, is the worktree's branch deleted?
Answer
No. Only the folder and Git's records of the worktree are removed. The branch and its commits stay in the repository. Delete it with git branch -d <branch> once it's merged.

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.