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.

Timeline comparing two ways to handle the feed bug. With one folder, my-app/ switches from feature-login to hotfix-bug and back, stashing login.py in between. With two folders, my-app/ stays on feature-login with login.py staged, and my-app-hotfix/ exists only during the fix.
Figure 1. Switching branches replaces the files in 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/ contains the .git folder, which holds the commits, branches, tags, stashes, remotes, and config shared by both folders. In my-app-hotfix/, .git is a file pointing back to my-app/.git. Each folder has its own files, staged changes, and HEAD.
Figure 2. The two folders before the fix is made. The repository is stored in 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-login again fails with 'feature-login' is already used by worktree at …. To look at its latest commit in another folder, use git worktree add --detach ../my-app-look feature-login.
  • A new worktree has only committed files. Anything in .gitignore, such as .env, .venv/, or node_modules/, has to be set up again in the new folder.
  • Removing a worktree deletes its ignored files. git worktree remove stops if the folder has modified or untracked files, but it deletes ignored files without asking. Save a customized .env before you run it.
  • Stashes are shared. Running git stash pop in 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, run git worktree repair from its new location.
  • Move and delete linked worktrees through Git. Use git worktree move and git worktree remove. If you already deleted a folder by hand, git worktree prune clears 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-page creates a local search-page branch 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 main and 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.


Reading Time

~3 min read

Published

Category

Tags

Stay in Touch