SwiftRelay

← 全部冲刺笔记

Run Two Coding Agents in Separate Git Worktrees

Agent 快速起步EN

Give two coding agents separate assignments, separate directories, and one integration checkpoint. This walkthrough pairs an implementation agent with a documentation agent, then brings their commits together for review. Keep yourself responsible for task boundaries, permissions, and the final merge.

1. Choose a Shared Starting Commit

Start in your existing repository. Run git status --short, then commit or otherwise preserve unfinished work before proceeding. The examples assume your intended starting branch is named main; substitute your actual branch name.

A linked worktree gives each agent its own working directory and index while sharing repository objects and branch references. Treat that as checkout separation, not a security sandbox. ([git-scm.com](https://git-scm.com/docs/git-worktree))

Choose two bounded assignments: agent A implements request validation, while agent B updates usage documentation. Agree on the expected validation behavior before either starts.

2. Create One Branch and Worktree per Agent

From the original repository, run these commands individually. Both branches start from the same main commit:

  • git worktree add -b agent/validation ../project-validation main
  • git worktree add -b agent/docs ../project-docs main
  • git worktree list

The first two commands create linked directories and new branches. The final command lists their paths and checked-out branches. Git normally refuses to check out the same branch in multiple worktrees, so keep the agent branches distinct. ([git-scm.com](https://git-scm.com/docs/git-worktree))

3. Prepare Separate Runtime Resources

Set up dependencies inside each directory using the project's documented installation procedure. For a Node project with a committed lockfile, that might be npm ci. Keep generated files out of commits unless repository policy requires them.

Assign different development-server ports and separate test databases, temporary directories, and output paths. Do not assume worktrees isolate external services. Supply required secrets through your normal secure environment mechanism, and never ask an agent to commit credentials.

4. Launch Each Agent from Its Assigned Directory

Open two terminals. In the first, run cd ../project-validation and launch your configured coding agent. In the second, enter ../project-docs from the original repository and launch the other agent. Ask each to report its current directory and branch before editing.

If you want a shared relay for the sessions, SwiftRelay works with Claude Code, Codex, and Gemini CLI, with pay-as-you-go, per-token billing. Sign up for SwiftRelay if that fits your setup, then configure each client through its supported provider settings without exposing keys in prompts.

5. Give Both Agents Explicit Editing Boundaries

Use concrete instructions rather than asking both agents to improve the project:

  • Agent A: Implement the agreed validation behavior and focused tests. Edit only the validation module and its tests. Do not change dependencies, documentation, or public interfaces.
  • Agent B: Document the agreed validation behavior and examples. Edit only the designated documentation files. Flag implementation questions rather than modifying source code.

Tell both agents not to switch branches, merge, rebase, or modify sibling directories. Require approval for dependency changes and destructive commands. If either assignment needs a shared file, pause and designate one owner.

6. Review and Commit Each Agent's Changes

Ask each agent to stop editing and report changed files, tests run, and unresolved questions. In each worktree, inspect git diff before staging and git diff --cached afterward; these show unstaged and staged changes respectively. ([git-scm.com](https://git-scm.com/docs/git-diff))

Stage only reviewed files and create a focused commit. Run the relevant tests yourself. Check that documentation describes implemented behavior, not merely the initial agreement.

7. Merge Both Branches into an Integration Branch

Return to the clean original repository. Run git switch -c integration/two-agents main, then merge agent/validation and agent/docs separately using git merge --no-ff --no-edit followed by the branch name. Resolve any conflicts before continuing. ([git-scm.com](https://git-scm.com/docs/git-merge))

Run the full project checks on the combined result. Review interactions even when Git reports no conflicts, especially mismatches between validation rules and documented examples. Submit the integration branch through your normal review process.

8. Remove Worktrees After Preserving Their Commits

After integration is accepted and the agent directories are clean, run git worktree remove ../project-validation and git worktree remove ../project-docs. Removing a worktree does not delete its branch; handle branch cleanup separately under your retention policy. ([git-scm.com](https://git-scm.com/docs/git-worktree))

接着读

又来跑一圈?登录