GuidesUpdated

Git worktrees for parallel coding agents: commands and cleanup

Give each coding agent its own branch and directory with git worktree, check status, merge reviewed work and remove worktrees safely.

On this page

Two agent tabs in one checkout share one set of files. A Git worktree gives each code-changing session its own directory and branch on one repository history.

Canopy agent file claims and parallel-work context
Canopy agent file claims and parallel-work context

Check the repository before you start

Run git status and leave unrelated changes in the main checkout. Pick two independent outcomes and name branches by owner. If both agents must change the same public API, settle the interface first or run the tasks in sequence.

Create one worktree per change

From the repository root, git worktree add -b creates a branch and linked directory beside the main checkout. Open each in its own agent session. Canopy can launch supported CLIs in isolated worktrees and show each one's branch.

Give each preview its own port

Separate files don't mean separate ports, databases or services. Give each preview its own port, or run only the checkout you're inspecting. Canopy allocates workspace ports; your project may need more setup. Check how it handles secrets before copying them.

Review each branch before you merge

Run each worktree's tests and read its diff, then open a PR per branch or merge through your usual process. Worktrees stop agents overwriting each other's files. They don't prevent merge conflicts.

Remove a worktree only when it's clean

Check for uncommitted changes and processes using the directory. Git refuses to remove a worktree with modified or untracked files unless forced; don't force it until you know what those files are. Once merged and clean, remove it with Git. The branch is a separate decision.

Copyable resources

Copyable two-agent setup

Run from the main repository directory; rename paths and branches for your project.

git status --short
git worktree add -b agent/settings ../project-settings
git worktree add -b agent/auth-review ../project-auth-review
git worktree list
# Open ../project-settings in one agent session.
# Open ../project-auth-review in another.
# Use separate ports or dependencies where the project needs them.

Inspection and cleanup

Review and merge first; don't force-remove worktrees with unknown changes.

git -C ../project-settings status --short
git -C ../project-settings diff --stat
git -C ../project-auth-review status --short
git -C ../project-auth-review diff --stat
# After reviewed work is committed/merged and each checkout is clean:
git worktree remove ../project-settings
git worktree remove ../project-auth-review
git worktree list

Frequently asked questions

Can two worktrees use the same branch?

Git normally stops one branch being checked out in two worktrees at once. Give each code-changing agent its own branch.

Do worktrees eliminate merge conflicts?

No. They isolate working files. Conflicting edits or decisions still need integration and review.

More questions and answers →

Sources and further reading