Git worktree: add, list, remove, and one per coding agent
How git worktree works: add with -b, an existing branch or detached, list, remove, prune, move, lock, the one-branch rule, and one worktree per coding agent.
A git worktree is a second (or tenth) working folder attached to the same repository, each with its own checked-out branch, while the history, branches and config stay shared. Create one with git worktree add ../app-feature -b feature, see them all with git worktree list, and delete one with git worktree remove ../app-feature, which deletes the folder with any ignored files in it, such as .env. A branch can be checked out in only one worktree at a time. Because each worktree has its own files, it is the standard way to let several coding agents work on one repository at once: one worktree per agent, each with its own dependencies, .env and dev-server port, since git copies none of those. Git 2.56.1 is the current release as of December 2026.
What a worktree is
Every repository you clone already has one worktree: the folder you cloned into, which git’s git-worktree manual calls the main worktree. git worktree add creates linked worktrees: more folders, anywhere on disk, each checked out at its own branch or commit. A linked worktree has a small .git file instead of a .git directory, holding one line, gitdir: <path>, that points back into the main repository, where git keeps the worktree’s bookkeeping under .git/worktrees/.
That is the difference from cloning the repository twice. A second clone downloads and stores everything again and has its own branches, so a commit in one is invisible in the other until you push and fetch. A worktree shares the object store and the branches: commit in one, and the commit is already there in every other.
| Shared by every worktree | Separate in each worktree |
|---|---|
Commits and objects (the history) | The files on disk |
Branches, tags, remotes and the stash list (everything under refs/) | HEAD (which branch or commit is checked out) |
The repository config, .git/config | The index (what is staged) |
Hooks in .git/hooks | Bisect and rebase state |
.git/info/exclude | Untracked and ignored files: .env, node_modules, build output |
The manual’s rule is that all refs starting with refs/ are shared except those under refs/bisect, refs/worktree and refs/rewritten, and that pseudo refs such as HEAD are per worktree; the stash lives at refs/stash, so it follows the first rule. The hooks, config and info rows come from gitrepository-layout, which says a linked worktree uses the common directory’s copy of each. If one worktree needs its own config (a sparse checkout, say), turn on git config extensions.worktreeConfig true and write the setting with git config --worktree.
git worktree add
Run these from inside the repository. Each creates the folder you name and checks something out into it; when the path is already taken, git stops with '<path>' already exists.
A new branch, cut from your current HEAD:
git worktree add ../app-feature -b featureCopiedA new branch from somewhere else, such as the latest main on the remote:
git fetch origin
git worktree add ../app-feature -b feature origin/mainCopiedA branch that already exists:
git worktree add ../app-bugfix bugfix-123CopiedIf bugfix-123 exists only on the remote, the same command works: when exactly one remote has a branch of that name, git treats it as git worktree add --track -b bugfix-123 ../app-bugfix origin/bugfix-123.
A throwaway worktree on no branch at all (a detached HEAD at your current commit), for trying something or running tests without touching any branch:
git worktree add -d ../app-scratchCopiedTwo shortcuts and one caution. With no branch and no -b, git names the branch after the folder: git worktree add ../hotfix checks out a branch called hotfix, creating it from HEAD if it does not exist. --lock locks the new worktree as it is created. And -b refuses a branch name that already exists, while -B does not refuse: it resets that branch to the starting point, so any commits only on it are no longer on the branch. Git confirms each case on its first line, with Preparing worktree (new branch 'feature'), (checking out 'bugfix-123') or (detached HEAD 1a2b3c4).
One branch, one worktree
Git will not check out the same branch in two worktrees, because a commit in one would move the branch under the other’s feet. Trying it, with git worktree add, git switch or git checkout, stops with this in current Git:
fatal: 'main' is already used by worktree at '/home/you/app'CopiedOlder versions (2.40, for instance) word it 'main' is already checked out at '/home/you/app'. It most often bites with main: it is checked out in the main folder, so no linked worktree can switch to it. The fixes, in the order to try them:
- Work on a branch of your own in the linked worktree, which is the point of having one, and merge it into
mainfrom the main folder. - Look at main without taking it:
git switch --detach origin/maininside the worktree, or create the worktree with-d. - Free the branch by switching the other worktree to something else, or removing it.
--force on add, or --ignore-other-worktrees on switch, puts the branch in both places anyway. Then a commit in either folder moves the branch while the other folder’s files and index still reflect the old commit, and that folder shows the difference as changes it never made. With agents committing on their own, leave both flags alone.
list, remove, prune and the rest
git worktree list shows the main worktree first, then each linked one with its commit and branch, and marks those that are locked or prunable:
/home/you/app 4f1c2ab [main]
/home/you/app-feature 9e8d7c6 [feature]
/home/you/app-scratch 4f1c2ab (detached HEAD)
/mnt/usb/app-old 77aa310 [old-work] locked
/home/you/app-gone 12cd34e [gone] prunableCopiedgit worktree list --porcelain gives the same in a format the manual promises stays stable across versions, for scripts and for an agent that needs to find its siblings; --verbose adds the reason behind each mark.
Remove a worktree you are done with, running the command from another worktree such as the main folder, so your shell is not left in a directory that no longer exists. Before you do, copy out any ignored files you want to keep: a plain remove deletes them. The manual’s rule is that only a clean worktree, with “no untracked files and no modification in tracked files”, can be removed, and it says nothing of ignored files. In git’s source, the cleanliness check runs git status --porcelain without --ignored, which does not list ignored files, and then deletes the whole folder, so a worktree whose only local content is a .env and node_modules counts as clean and goes with them. Anthropic’s Claude Code docs warn of the same for its own cleanup. Keep the master copy of any secrets file outside the worktrees.
git worktree remove ../app-featureCopiedGit refuses if the worktree has modified tracked files or untracked files, with '<path>' contains modified or untracked files, use --force to delete it. The next command deletes the folder anyway. Its uncommitted changes, untracked files and ignored files are deleted for good; commit or copy out what you want first:
git worktree remove --force ../app-featureCopiedWith either form the branch and its commits stay, since they live in the shared repository; delete the branch separately with git branch -d feature once it is merged. The main worktree cannot be removed at all (is a main working tree).
Prune cleans up after a worktree folder deleted with rm -rf instead of remove. Git keeps its record, lists it as prunable, and can go on reporting its branch as in use by a folder that no longer exists. Preview, then prune:
git worktree prune -n -v
git worktree prune -vCopiedLeft alone, git gc does the same for records older than three months (gc.worktreePruneExpire, per the git-gc manual).
| Command | What it does |
|---|---|
git worktree move ../app-feature ../work/app-feature | Moves a linked worktree and updates git’s records. It cannot move the main worktree, or one containing submodules. |
git worktree lock --reason "on USB disk" /mnt/usb/app-old | Stops prune, move and remove from touching it; meant for a worktree on a disk or share that is not always mounted. A locked worktree needs --force twice to remove or move. |
git worktree unlock /mnt/usb/app-old | Releases the lock. |
git worktree repair | Reconnects the repository and its worktrees after a folder was moved without git worktree move: run it in the main folder if that moved, inside a linked worktree that moved, or from any worktree with the new paths of several moved ones as arguments. |
If a command says a path is a missing but already registered worktree, a folder was deleted without remove and git still holds its record; git worktree prune clears it.
Where to put worktrees
Git does not mind; the folder can be anywhere. Two layouts work well:
- Beside the repository:
~/code/app,~/code/app-feature,~/code/app-bugfix. Nothing inside the repository changes, and editors, file watchers and test runners in one folder never see another’s files. - In an ignored folder inside it, such as
.worktrees/feature. Everything for the project stays under one directory, but the folder must be ignored, or the main worktree shows every worktree’s files as untracked. To ignore it without committing anything, add.worktrees/to.git/info/exclude, which every worktree shares. Tools that walk the whole tree (a test runner, a linter, a language server) may then descend into the nested worktrees unless they skip ignored folders.
The coding agents with worktree support of their own pick a place for you, and each picks a different one (the table further down). Two habits help whichever you use: name the folder after the branch, so git worktree list reads at a glance, and avoid worktrees of a repository with submodules where you can. The manual’s own BUGS section says “the support for submodules is incomplete. It is NOT recommended to make multiple checkouts of a superproject.”
One worktree per coding agent
Two agents in one folder edit the same files, run the same test suite over each other’s half-made changes, and stage into one index, so one agent’s commit can sweep up the other’s work. Give each agent its own worktree on its own branch and those collisions stop: each has its own files, index and HEAD, and every commit lands in the one shared repository, ready to merge or push. How many agents one machine carries is a question of memory and CPU, which how many coding agents fit on one server works through.
cd ~/code/app
git fetch origin
git worktree add ../app-auth -b agent/auth origin/main
git worktree add ../app-billing -b agent/billing origin/mainCopiedThen start one agent in each folder, in its own terminal or tmux window. What git does not give each new folder is everything it does not track, and that is what usually stops the second agent:
| Missing in a new worktree | What to do |
|---|---|
Dependencies ( node_modules, a virtualenv, compiled output) | Install in each worktree. With pnpm this is cheap: packages are hard-linked or cloned from one store on the disk, so ten worktrees do not mean ten copies. Do not symlink one node_modules into several worktrees on different branches: their lockfiles drift apart and an install in one changes it for all. |
.env and other ignored config | Copy it in, or have the hook below do it. Keep the master copy in the main folder or outside the repository, because removing a worktree deletes its ignored files. |
A free port | Give each worktree’s dev server its own (next section). |
A database of its own | Agents running migrations on different branches against one local database undo each other’s schema. Point each worktree’s .env at its own database name. |
Set up each new worktree automatically. Git runs the post-checkout hook after git worktree add (unless you pass --no-checkout), in the new worktree’s root, with an all-zero commit as its first argument (the githooks manual). Hooks are shared, so one hook serves every worktree. This one copies .env from the main folder into each new worktree that lacks one; save it as .git/hooks/post-checkout in the main folder:
#!/bin/sh
# Run only for a fresh checkout (git worktree add or clone),
# whose first argument is all zeros.
case "$1" in *[!0]*) exit 0 ;; esac
main=$(git worktree list --porcelain | sed -n '1s/^worktree //p')
if [ -f "$main/.env" ] && [ ! -e .env ]; then
cp "$main/.env" .env
fiCopiedThen make it executable:
chmod +x .git/hooks/post-checkoutCopiedAdd your install command after the copy if you want dependencies in place before an agent starts; it then runs on every git worktree add, which makes each add slower. The hook runs for worktrees you create with git. Whether an agent’s own worktree feature runs it is not documented by any of the three below, and Claude Code and Codex have .worktreeinclude for the copying part instead.
Dev servers and ports
Two dev servers cannot listen on one port, and the frameworks handle the clash differently. Vite (default 5173) moves to the next free port on its own unless strictPort is set, which means the second agent’s server may come up on 5174 while the agent goes on testing localhost:5173, the first agent’s server and branch; that follows from the documented fallback, and it is why fixed ports per worktree are worth the minute. Next.js (default 3000) takes -p or the PORT variable, and its CLI reference warns that PORT cannot be set in .env. So in the second worktree, for a Next.js app:
npx next dev -p 3001 -H 127.0.0.1CopiedThe -H 127.0.0.1 matters on a server. next dev listens on 0.0.0.0 by default, every network interface, so on a machine with a public address and no firewall in front, each agent’s dev server is reachable from the internet. Bind to localhost and open it through an SSH tunnel or a private network such as Tailscale; opening a localhost app on a remote server walks through both. Tell each agent its port in the prompt or the project’s agent instructions, so it tests the server it started.
Worktrees in Claude Code, Codex and OpenCode
All three can create worktrees themselves, as of December 2026, each in its own place:
| Agent | How to start one | Where it goes | Ignored files |
|---|---|---|---|
Claude Code | claude --worktree <name> (or -w); ask it to “work in a worktree” mid-session | .claude/worktrees/<name>/ in the repository, on a new branch worktree-<name> cut from the remote’s default branch | Copied if listed in .worktreeinclude |
Codex CLI (0.174.0) | codex --worktree, also on codex exec and codex fork; /worktree in a session | A “managed Git worktree”; the CLI docs do not say where | Not documented for the CLI |
Codex in the ChatGPT desktop app | The app’s worktree option for a chat | $CODEX_HOME/worktrees, detached HEAD; keeps the 15 most recent | Copied if listed in .worktreeinclude |
OpenCode (v2.4.4) | opencode run -w [name] "prompt"; the agent’s worktree_create tool | OpenCode’s data directory, under worktree/ plus six characters of the project ID; set worktree.directory in the config to change it | Not documented |
Claude Code removes a clean worktree from an unnamed session when you exit and asks about one with work in it. Its worktrees page warns that gitignored files such as .env do not count as work, “so you can lose them at exit”, the same trap as git worktree remove. Add .claude/worktrees/ to .gitignore. The settings that change the base branch, share directories or check out only part of a monorepo are in the Claude Code worktrees guide.
Codex takes no name with --worktree (it is a plain switch), and its CLI reference says that on codex exec it cannot be combined with resume, review, --ephemeral or --ignore-user-config. /worktree can carry the current conversation into a new worktree, and its Browse worktrees choice lists existing ones with the chats that own them. In the desktop app, per its worktrees page, a managed worktree is deleted when you archive its chat (unless the chat is pinned or still running), and to keep the work you turn it into a branch with Create branch here. Because those worktrees sit on a detached HEAD, they never hold a branch another worktree needs. Pushing the branch and opening the pull request is Codex and GitHub.
OpenCode’s -w belongs to opencode run, the one-prompt command; its tools page gives the agent worktree_create, worktree_list and worktree_remove, and the last needs the edit permission and discards uncommitted changes when called with force.
Whichever made it, a worktree is an ordinary git worktree: git worktree list shows the agents’ ones beside yours, and remove and prune clean them up when the agent’s own cleanup did not. Committing and pushing from Claude Code is covered in Claude Code and git.