Sub topic 3
Creating, listing and removing worktrees
This chapter assumes you are comfortable with commits, branches and merges, and that you have a repository with a main branch. It covers the four git worktree subcommands you will use most: add, list, remove and prune. It also covers lock, which protects a worktree from pruning.
Creating a worktree
git worktree add takes a path and a starting point. It creates the directory, checks out files into it, and links it to the repository you ran the command from.
git worktree add -b agent/auth ../myrepo-auth main
git worktree add -b agent/search ../myrepo-search main
git worktree listEach command does three things:
- It creates a new directory at the path you gave. Here the path is a sibling of the repository, so it stays outside the main working tree.
- It creates a new branch, named by
-b, that starts atmain. - It checks that branch out in the new directory.
Putting worktrees next to the repository, not inside it, keeps Git from seeing the new directory as untracked files in your main checkout.
Listing worktrees
git worktree list prints one line per worktree: its path, the commit it is on, and its branch.
/home/dev/myrepo 3f2a9c1 [main]
/home/dev/myrepo-auth 3f2a9c1 [agent/auth]
/home/dev/myrepo-search 3f2a9c1 [agent/search]All three lines show the same commit because both new branches start from main. As each agent commits, its line moves to a new commit hash. Use git worktree list --porcelain when a script needs to parse the output.
Detached worktrees
A branch can be checked out in only one worktree at a time. If you want another checkout of the same commit, use a detached worktree, or give it a new branch.
git worktree add --detach ../myrepo-scratch main
git worktree add ../myrepo-main2 main # fails if main is already checked out elsewhereThe first command works because a detached worktree is not tied to any branch. The second fails while main is checked out in your original directory. Git refuses so that two worktrees cannot move the same branch at once. The next chapter covers these rules in more detail.
Locking a worktree
Git records each linked worktree in the main repository's .git/worktrees directory. If a worktree sits on a removable drive or network share, Git may decide it is gone and prune its record. To prevent that, lock it.
git worktree lock --reason "on external drive" ../myrepo-auth
git worktree unlock ../myrepo-authA locked worktree is skipped by prune and refused by remove until you unlock it. You rarely need this for local agent runs, but it is useful for long-lived worktrees.
Removing a worktree
When an agent's work is merged or abandoned, remove the worktree, then the branch.
git worktree remove ../myrepo-auth
git branch -d agent/auth
git worktree prunegit worktree removedeletes the directory and the worktree's record. It refuses if the worktree has uncommitted changes or untracked files. Add--forceonly after you have checked that nothing in it is worth keeping.git branch -ddeletes the branch only if it has been merged. Removing a worktree never deletes its branch.git worktree pruneclears records of worktrees whose directories were deleted by hand, for example withrm -rf.
Common errors
| Message or symptom | Cause | Fix |
|---|---|---|
| `fatal: 'main' is already checked out` | The branch is in use by another worktree | Use `--detach` or create a new branch with `-b` |
| `fatal: a branch named 'agent/auth' already exists` | `-b` was used with a name that exists | Use `-B` to reset it, or choose a new name |
| `cannot remove a worktree with modifications` | Uncommitted or untracked files | Commit or stash, or use `--force` deliberately |
| Deleted worktree still appears in the list | The directory was removed by hand | Run `git worktree prune` |
Exercise
You create ../myrepo-auth with git worktree add -b agent/auth ../myrepo-auth main. The agent finishes and you merge its branch. You run git worktree remove ../myrepo-auth. Does the branch agent/auth still exist, and which command deletes it safely?
Show answer
Yes, the branch still exists, because removing a worktree deletes only its directory and its record. Run git branch -d agent/auth. It deletes the branch only if it is fully merged, so unmerged work is not lost.