A Git worktree is a folder with a branch or commit checked out, and one repository can have several. Your clone is the main worktree. Every folder you add with git worktree add is a linked worktree, so you can have two branches open at once without cloning the repository again. See the Git worktree documentation.
What problem do they solve?
You're building a login form in my-app/, on a branch called feature-login. You've staged login.py, but it isn't ready to commit. Then someone reports a broken RSS feed, and the fix has to start from main.
With one folder, you have to put the login form away first. If you run git switch straight away, Git brings the staged login.py with you, where it could end up in the feed fix. If switching would overwrite your edits, Git refuses to switch. The usual answer is to stash the work, fix the feed, switch back, and restore the stash. Git switch documentation
A worktree lets you leave the login form as it is and fix the feed in another folder.
my-app/ twice. Adding a worktree leaves them alone.How do I use one?
From my-app/, create a branch for the fix and a folder to hold it:
git worktree add -b hotfix-bug ../my-app-hotfix main
-b hotfix-bug names the new branch, ../my-app-hotfix is the new folder, and main is where the branch starts. Open that folder in a second editor window and work as usual:
cd ../my-app-hotfix
# Fix and test feed.py
git commit -am "Fix RSS feed"
git worktree list shows each folder, its current commit, and its branch:
/home/me/my-app c93b3ac [feature-login]
/home/me/my-app-hotfix 94e8809 [hotfix-bug]
Once the fix is committed, go back and remove the extra folder:
cd ../my-app
git worktree remove ../my-app-hotfix
This deletes the folder and keeps the hotfix-bug branch, ready to push or merge. In my-app/, login.py is still staged.
What do worktrees share?
Worktrees share one repository. A commit made in my-app-hotfix/ is visible from my-app/ immediately, with no push or pull. Each worktree has its own files, its own staging area (the changes selected for the next commit), and its own HEAD (the branch or commit it has checked out).
my-app/.git, and my-app-hotfix/ contains a small file that points to it.What should I watch out for?
- A branch can be checked out in only one worktree at a time. Trying to open
feature-loginagain fails with'feature-login' is already used by worktree at …. To look at its latest commit in another folder, usegit worktree add --detach ../my-app-look feature-login. - A new worktree has only committed files. Anything in
.gitignore, such as.env,.venv/, ornode_modules/, has to be set up again in the new folder. - Removing a worktree deletes its ignored files.
git worktree removestops if the folder has modified or untracked files, but it deletes ignored files without asking. Save a customized.envbefore you run it. - Stashes are shared. Running
git stash popin one folder can apply changes you stashed in the other. - Linked worktrees depend on the main one. They stop working if you delete
my-app/, because the repository is inside it. If you only moved it, rungit worktree repairfrom its new location. - Move and delete linked worktrees through Git. Use
git worktree moveandgit worktree remove. If you already deleted a folder by hand,git worktree pruneclears Git's record of it.
When is a worktree worth it?
- Interruptions. You have unfinished work and need another branch, as in the feed example.
- Code review. You want to run a colleague's branch while keeping your own checked out. After
git fetch,git worktree add ../my-app-review search-pagecreates a localsearch-pagebranch that tracks the remote one, provided you don't have that branch yet and only one remote does. - Comparing versions. You want to run the same failing test on
mainand on your fix, with both versions on disk. - Parallel work. A long build, a test run, or a coding agent can use one folder while you edit files in another.
If you have nothing uncommitted and only need one branch at a time, git switch is simpler.