# Git worktree merge conflicts with AI agents: why worktrees don't stop them, and how to see them early

<!--
title: "Git worktree merge conflicts with AI agents: why worktrees don't stop them, and how to see them early · Cork"
description: "Git worktrees keep parallel AI coding agents out of each other's files, not out of each other's code. Why the merge conflicts still come, the ones git never reports, and a one-command check that finds them while the agents are still working."
canonical: "https://corkcli.com/blog/git-worktree-merge-conflicts-ai-agents"
last_updated: "2026-11-18"
published: "2026-11-18"
author: "Kevin Vera"
-->

Git worktrees keep parallel AI coding agents out of each other's files, not out of each other's code. Why the merge conflicts still come, the ones git never reports, and a one-command check that finds them while the agents are still working.

By Kevin Vera · November 18, 2026 · [Guides](https://corkcli.com/blog/c/guides) · 6 min read

You gave every agent its own git worktree. The agents stopped overwriting each other's files, and then the merge broke anyway. That is not a sign you set worktrees up wrong. A worktree decides where an agent's edits happen, not what they touch, so two agents can still change the same code and only find out when the second branch merges. [GitButler](https://corkcli.com/blog/cork-vs-gitbutler)'s Scott Chacon put it in one line: "The worktrees are separate, so you can easily create merge conflicts between them without knowing."

The fix is not to give up worktrees. It is to find the overlap while the agents are still working, when it costs a sentence to fix, instead of at merge time, when it costs a rewrite.

## Why worktrees don't prevent merge conflicts

Claude Code's documentation describes a worktree as "a separate working directory with its own files and branch, sharing the same repository history and remote as your main checkout." Each agent gets its own copy of the files, so no agent can overwrite another's uncommitted work. But each agent's work still ends up as a branch of the same repository, and two branches that change the same lines conflict when they meet, whichever folder they were written in.

Parallel agents make this more likely than parallel people do:

- **They go to the same hot spots.** Route tables, schemas, config, index files and lockfiles are where most features have to register themselves.
- **Their runs are long.** A branch drifts further from main with every hour it isn't merged.
- **Nobody looks across.** Each agent sees its own worktree. Nothing tells it that another one is rewriting the function it depends on.

Trigger.dev's CTO described the result: "Two agents refactor the same utility function in different directions. Now you're resolving conflicts in code you didn't write."

## The four kinds you will hit

| Kind | Example | Does git report it? |
|---|---|---|
| Same lines | Both agents add a route in the same block of `router.ts` | Yes, as a conflict when the second branch merges |
| Lockfiles and generated files | Both agents add a dependency, so both rewrite `package-lock.json` | Yes, often hundreds of conflicting lines |
| Same idea, different lines | One agent renames `getUser` to `fetchUser`; another adds a new call to `getUser` in another file | No. The merge is clean and the build breaks. This is a [semantic merge conflict](https://corkcli.com/blog/semantic-merge-conflict) |
| Same slot, different files | Both agents add a database migration numbered `0042`, under different names | No. Two migrations claim the same number |

The first two are loud. The last two are the expensive ones, because nothing fails until the code runs.

## See the conflict before the merge

Git can do a merge in memory without touching any working folder. Since Git 2.38, `git merge-tree --write-tree` takes two branches, merges them, and exits with status 1 if they conflict:

```bash
git merge-tree --write-tree --name-only agent-1 agent-2
```

That is enough to check every agent's branch against every other one, and against `main`, whenever you like:

```bash
# every branch checked out in a worktree, checked against every other
branches=$(git worktree list --porcelain | sed -n 's|^branch refs/heads/||p')
for a in $branches; do
  for b in $branches; do
    [ "$a" \< "$b" ] || continue
    out=$(git merge-tree --write-tree --name-only "$a" "$b") && continue
    echo "$a and $b conflict in:"
    echo "$out" | awk 'NR > 1 && NF == 0 { exit } NR > 1 { print "  " $0 }'
  done
done
```

Two limits. It sees only what the agents have committed, so ask them to commit small and often. And a clean answer only means git found no overlapping lines. For the semantic kind you have to build the merged result. You can do that without touching main: turn the merged tree into a throwaway commit, check it out in a temporary worktree, and run the tests there.

```bash
tree=$(git merge-tree --write-tree agent-1 agent-2) &&
commit=$(git commit-tree "$tree" -p agent-1 -p agent-2 -m "trial merge") &&
git worktree add --detach ../trial "$commit"
# run the build and tests in ../trial, then:
git worktree remove ../trial
```

## Make the conflicts smaller when they come

- **Land one branch at a time.** Rebase it onto the latest main, run the full test suite on the result, then merge. Every later branch rebases onto what landed.
- **Keep the hot spots with one agent.** Give one agent the route table or the schema, or have the others ask it for changes there.
- **Don't merge lockfiles by hand.** Take one side and rerun the install, which rebuilds the file from the merged `package.json`.
- **Name migrations by timestamp, not by sequence number,** so two agents can't claim the same slot.
- **Tell the other sessions what landed.** Claude Code's cross-session messaging (v2.1.224 on macOS and Linux) lets Claude "tell the other sessions what landed" when they work the same repository in separate worktrees. It sends plain text between sessions on the same machine, when Claude decides to.

## Where Cork fits

Everything above happens after an agent has committed. Cork works at the moment of the edit. Its hooks report every write as it happens, in every worktree on the machine and, on the paid plan, on teammates' machines too. So the overlap shows up on the agent's next step instead of at the merge:

- **When an agent is about to change code another agent is changing right now,** the [write gate](https://corkcli.com/blog/write-gate) holds the edit until it has the other agent's version.
- **When two agents need the same numbered file,** the gate stops the second one and reserves the next free number for it.
- **When an agent renames a function or changes its signature,** that becomes a [semantic lint](https://corkcli.com/blog/semantic-lint) finding for every agent that calls it, while they are still working.
- **When a branch is ready,** landing rebases it onto what teammates committed, reruns the tests and pushes.

Cork does not replace a test run on the merged result, and it does not isolate ports or databases the way separate environments do. It shortens the time between two agents touching the same code and someone noticing. You can keep your worktrees; Cork works the same in one checkout or in many.

## Sources

Checked 18 November 2026:

- Scott Chacon, "[Git Worktrees](https://corkcli.com/blog/git-worktrees-parallel-ai-coding-agents) and GitButler", GitButler blog: https://blog.gitbutler.com/git-worktrees
- Eric Allam, "We ditched worktrees for Claude Code. Here's what we use instead", Trigger.dev, 16 April 2026: https://trigger.dev/blog/parallel-agents-gitbutler
- Claude Code docs, "Run parallel sessions with worktrees": https://code.claude.com/docs/en/worktrees
- Claude Code docs, "Message your other Claude Code sessions": https://code.claude.com/docs/en/cross-session-messaging
- Git documentation, `git merge-tree`: https://git-scm.com/docs/git-merge-tree

## Frequently asked questions

### Do git worktrees prevent merge conflicts between AI agents?

No. Worktrees stop agents from overwriting each other's uncommitted files. Two agents that change the same lines in their own worktrees still conflict when the second branch merges, and changes that merge cleanly can still break the build.

### How do I check whether two worktrees will conflict before I merge them?

Run `git merge-tree --write-tree --name-only branch-a branch-b` (Git 2.38 or later). It merges in memory, touches no working folder, exits with status 1 on a conflict and lists the conflicting files. It sees only committed work.

### Why does the code break after a merge with no conflicts?

Because git compares lines, not meaning. If one agent renames a function and another adds a call to the old name in a different file, both changes merge cleanly and the build fails. Build and test the merged result before it lands, or use a tool that tells agents about renames and signature changes while they work.

### Should I stop using worktrees for parallel agents?

Not because of conflicts alone. Worktrees are still the simplest way to keep agents' files apart. Pair them with an early conflict check, one-at-a-time landing, and something that makes agents aware of each other's changes. If the cost of setting up each worktree is what hurts, see [alternatives to git worktrees for Claude Code](https://corkcli.com/blog/git-worktrees-alternatives-claude-code).

## Related reading

- [Parallel Claude Code agents: what still breaks after you add worktrees](https://corkcli.com/blog/parallel-claude-code-agents-what-still-breaks): Worktrees stop parallel Claude Code sessions from overwriting each other's files. What they don't stop: merge conflicts, clean merges that don't build, decisions that never reach the other session, shared memory and config, ports and databases, setup cost and git lock races. The fix for each.
- [Cork vs git worktrees: separate folders, or agents that know about each other](https://corkcli.com/blog/cork-vs-git-worktrees): Cork vs git worktrees for parallel AI coding agents: worktrees isolate files while agents work, Cork coordinates them so the work still fits at merge.
- [What data an agent coordination tool needs (and what it should not see)](https://corkcli.com/blog/agent-coordination-tool-data-privacy): What data an AI agent coordination tool needs to stop agents overwriting each other, what it should never see, and the questions to ask before you install one.
- [How to stop Claude Code agents from overwriting each other's changes](https://corkcli.com/blog/stop-claude-code-agents-overwriting-changes): Why parallel Claude Code agents overwrite each other's edits, and how to stop it with worktrees, file ownership, deny rules, PreToolUse hooks and a write gate.

[All articles](https://corkcli.com/blog.md) · [Cork in Markdown](https://corkcli.com/index.md) · [llms.txt](https://corkcli.com/llms.txt)

This page in HTML: https://corkcli.com/blog/git-worktree-merge-conflicts-ai-agents
