deniz.in

Markets

Weather

Loading weather

· via dev.to (home feed)

Git worktrees let AI coding agents work in parallel without file collisions

A dev.to walkthrough shows how git worktrees give each AI coding agent its own directory and branch, letting parallel tasks merge locally with no push or pull.

Git worktrees let AI coding agents work in parallel without file collisions

One repo, too many agents

A single AI coding session in a tool like Claude Code or Codex usually works fine. The trouble, as a recent dev.to post by servatj explains, begins when you want a second task running at the same time — login in one terminal, payments in another. Both agents share one working directory and one checked-out branch, so two automated processes end up editing the same files simultaneously. The author's solution is a Git feature that has existed for years but rarely features in everyday workflows: git worktree.

What worktrees give you

A worktree is an extra working directory backed by the same repository. Each directory checks out its own branch while all of them share the same objects, commits and branch list. The post demonstrates the setup from the main checkout:

git worktree add ../integration -b integration main git worktree add ../feature-login -b feature/login main git worktree add ../feature-payments -b feature/payments main

The result is a project folder containing the main checkout alongside separate directories for integration, feature/login and feature/payments. Each agent gets one folder and one terminal, and none of them touches another agent's files.

Merging without push or pull

The part the author did not expect is that the team-workflow instinct of pushing a finished branch and pulling it elsewhere is unnecessary here. All worktrees belong to the same repository on the same machine, so Git already knows every branch locally. Once the login agent commits, the integration worktree merges feature/login directly, runs the test suite, and eventually merges integration back into main. No push or pull appears anywhere in that loop.

Branches can also be merged while their worktrees still exist. Git's only restriction is that the same branch cannot be checked out in two worktrees at once — which, as the author points out, is precisely the protection you want when multiple agents are involved.

Cleanup is deliberately gentle. Rather than deleting a folder by hand, git worktree remove deletes the directory and updates Git's internal registry; if a folder was already removed manually, git worktree prune repairs the bookkeeping afterwards. Deleting the branch with git branch -d completes the cycle.

Where GitHub fits in

For agents collaborating on one machine, GitHub has no role at all — Git itself is the coordination mechanism. The remote earns its place once people join the loop, providing pull requests, code review, CI and an audit trail. The author frames the result as two separate planes: a local plane where agents commit, merge into an integration branch and run tests, and a remote plane where humans push, review and let CI verify.

A detail the author highlights is that the integration agent never reads another agent's folder or copies its code. It only ever knows branch names, so Git effectively acts as the communication channel between agents.

Runtime is still shared

Worktrees isolate files and branches, not runtime. Two agents that each run a dev server and a Playwright suite will compete for port 3000, hit the same test database and consume a substantial amount of memory per headless browser. The workaround the author uses today is unglamorous: each worktree receives its own port through an env file written at creation time, which Playwright reads for its base URL. Isolating test databases, taming multiple browser suites and deciding whether integration should be the only worktree allowed to run end-to-end tests remain open questions the author plans to address in a follow-up piece.

Why it matters

Running several AI coding agents in parallel is quickly becoming a normal way to work, and most of the friction is mundane: conflicting edits, shared ports, tangled branches. This pattern needs no new tooling. It combines a long-standing Git feature with the terminals and agents developers already have, and it keeps the machine-local agent loop cleanly separated from the human review loop, so pull requests and CI remain untouched. The remaining gaps around runtime isolation and shared resources are real, but the core workflow is something an individual developer or a team could adopt today with a handful of commands.

  • #git
  • #ai-agents
  • #developer-tools
  • #workflow
  • #claude-code

Related posts