Sub topic 3
Creating, listing and removing worktrees
This chapter covers the git worktree subcommands you will use to give each agent its own directory. It assumes you know everyday Git (commit, branch, merge, diff) and have read the previous chapter on how a linked worktree relates to its repository. The next chapter covers branch rules in more detail. Here the focus is the commands and what they do on disk.
The command family
All worktree operations go through git worktree. These are the subcommands you will use:
| Subcommand | Purpose |
|---|---|
| `add` | Create a new linked worktree |
| `list` | Show every worktree of the repository |
| `lock` / `unlock` | Protect a worktree from pruning, or remove that protection |
| `move` | Relocate a linked worktree |
| `remove` | Delete a linked worktree and its metadata |
| `prune` | Delete metadata for worktrees whose directories no longer exist |
| `repair` | Fix links between the repository and worktrees after a manual move |
You can run these commands from the main checkout or from any linked worktree. They all act on the same shared repository.
Creating a worktree
The general form is:
git worktree add [-b <new-branch>] [--detach] <path> [<commit-ish>]<path> is the directory to create. <commit-ish> is the branch, tag or commit to start from. The most common form for agent work creates a new branch and a new directory in one step:
git worktree add -b agent/auth ../myrepo-auth main
git worktree add -b agent/search ../myrepo-search main
git worktree listEach add does three things:
- It creates the directory (
../myrepo-auth) and any missing parent directories. - It creates the branch
agent/authpointing at the same commit asmain. - It checks that branch out into the new directory.
The path is relative to the directory where you run the command, not to the repository root. Placing worktrees next to the repository, as in ../myrepo-auth, keeps them out of the main checkout. A worktree nested inside the main checkout would show up as untracked files in git status there.
The target directory must not exist, or it must be empty. Git refuses to add a worktree into a directory that already holds files.
What `add` leaves on disk
The new directory contains a checkout of the files, plus a small file named .git. Unlike the .git directory in the main checkout, this one is a plain text file that points back to the repository:
gitdir: /home/dev/myrepo/.git/worktrees/myrepo-authThe repository keeps per-worktree data, such as the worktree's HEAD and index, under .git/worktrees/<name>. Everything else is shared: the object database, the branches and tags, and the configuration. A commit made in one worktree is visible to the others immediately, with no fetch or push.
Shortcuts and omitted arguments
You can leave out parts of the command, and Git fills in defaults.
- If you omit
<commit-ish>and pass-b, the new branch starts from the currentHEADof the directory you ran the command in. That may not bemain, so name the base explicitly in scripts. - If you omit both
-band<commit-ish>, Git looks for a branch named after the last component of the path. If that branch exists, it is checked out. If not, Git creates it fromHEAD. --detachchecks out a commit with no branch at all. Use it for throwaway work.
git worktree add --detach ../myrepo-scratch main
git worktree add ../myrepo-main2 main # fails if main is already checked out elsewhereThe second command fails because a branch can be checked out in only one worktree at a time. Git enforces this so that two directories never advance the same branch. The next chapter explains the rule and its exceptions.
<br>
Listing worktrees
git worktree list prints one line per worktree, starting with the main one:
git worktree list/home/dev/myrepo 3f2a9c1 [main]
/home/dev/myrepo-auth 3f2a9c1 [agent/auth]
/home/dev/myrepo-search 3f2a9c1 [agent/search]
/home/dev/myrepo-scratch 3f2a9c1 (detached HEAD)Each line shows the path, the abbreviated commit that HEAD points to, and the branch in brackets. A detached worktree shows (detached HEAD) instead of a branch. Git marks a worktree locked or prunable when that applies, and --verbose adds the reason on a separate line.
For scripts, use the porcelain format. Its field names stay stable across Git versions, and the output is not shaped for human reading:
git worktree list --porcelainworktree /home/dev/myrepo
HEAD 3f2a9c1d5e7b8a9c0d1e2f3a4b5c6d7e8f9a0b1c
branch refs/heads/main
worktree /home/dev/myrepo-auth
HEAD 3f2a9c1d5e7b8a9c0d1e2f3a4b5c6d7e8f9a0b1c
branch refs/heads/agent/authRecords are separated by a blank line. Parse them by reading worktree, branch and HEAD lines. Paths containing newlines would break line-based parsing, so recent Git versions also accept -z, which terminates fields with NUL characters.
Locking a worktree
Git treats a worktree whose directory has vanished as stale and removes its metadata when it prunes. That is a problem if the worktree lives on a removable drive or a network share that is sometimes unmounted. Locking prevents pruning:
git worktree lock --reason "on external drive" ../myrepo-auth
git worktree unlock ../myrepo-authA locked worktree also cannot be moved or removed without extra force. For agent work, locking is useful when a long-running agent owns a worktree and you want a guard against a cleanup script deleting it by accident.
Moving a worktree
If you must relocate a linked worktree, use git worktree move rather than mv:
git worktree move ../myrepo-auth ../agents/authGit updates both sides of the link, the .git file in the worktree and the metadata in the repository. You cannot move the main worktree this way. If you already moved a directory with mv, run git worktree repair from the main checkout, passing the new path, to restore the link.
Removing a worktree
When an agent's work is merged or abandoned, remove its worktree:
git worktree remove ../myrepo-auth
git branch -d agent/auth
git worktree pruneThe three commands do different jobs:
git worktree removedeletes the directory and the metadata under.git/worktrees. It refuses if the worktree has uncommitted changes or untracked files. This protects an agent's unfinished work. It never deletes the branch.git branch -ddeletes the branch only if its commits are merged. If they are not, Git refuses, and-Dforces the deletion.git worktree pruneclears records for worktrees whose directories were deleted by hand, for example withrm -rf. After a properremove, there is nothing left to prune, so the command is harmless but usually unnecessary.
To discard a worktree with uncommitted changes, add --force. A locked worktree needs --force twice, or an unlock first. You cannot remove the main worktree.
What happens if you delete the directory by hand
Running rm -rf ../myrepo-auth leaves the repository believing the worktree still exists. git worktree list marks it prunable, and the branch stays checked out in a worktree that does not exist, so Git will not let you check it out elsewhere. git worktree prune fixes this. Use git worktree prune --dry-run --verbose first to see what it would delete. By default Git also prunes stale records automatically during garbage collection, after an expiry period set by gc.worktreePruneExpire.
The worktree lifecycle
The diagram summarizes the states a linked worktree moves through and the command behind each transition.
Common errors
| Message or symptom | Cause | Fix |
|---|---|---|
| `'main' is already checked out at ...` | The branch is in use by another worktree | Create a new branch with `-b`, or use `--detach` |
| `'../myrepo-auth' already exists` | The target directory is not empty | Pick a new path or remove the old directory |
| `a branch named 'agent/auth' already exists` | `-b` was used for an existing branch | Omit `-b` to check it out, or use `-B` to reset it |
| `contains modified or untracked files, use --force to delete it` | `remove` found uncommitted work | Commit or stash it, or pass `--force` |
| Worktree shown as `prunable` | Its directory was deleted by hand | Run `git worktree prune` |
Be careful with -B. It resets an existing branch to the given commit, which can discard commits on that branch that nothing else references.
Exercise
You run git worktree add -b agent/billing ../myrepo-billing main, an agent commits in that directory, and you then run git worktree remove ../myrepo-billing followed by git branch -d agent/billing. The second command fails. Why, and what are two ways to proceed?
Show answer
git worktree remove deletes only the directory and its metadata, so the branch and the agent's commits remain. git branch -d refuses because agent/billing has commits that are not merged into the current branch. You can merge the branch first (for example git merge --no-ff agent/billing from main) and then run git branch -d, or, if the work is not wanted, delete the branch with git branch -D agent/billing.
Exercise
A teammate deleted a worktree directory with rm -rf. git worktree list shows it as prunable, and git worktree add ../new main fails because the branch it used is considered checked out. Which command fixes the situation, and how can you preview its effect?
Show answer
Run git worktree prune. Preview it with git worktree prune --dry-run --verbose, which lists the records it would delete without changing anything.