---
title: "Humans and AI agents on one branch: how to coordinate · Cork"
description: "Share a branch between developers and AI coding agents without lost work: who owns which files, commit and rebase rhythm, safe pushes, and review rules."
canonical: "https://corkcli.com/blog/humans-and-ai-agents-same-branch"
last_updated: "2026-10-03"
published: "2026-10-03"
author: "Kevin Vera"
---

# Humans and AI agents on one branch: how to coordinate

Share a branch between developers and AI coding agents without lost work: who owns which files, commit and rebase rhythm, safe pushes, and review rules.

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

To coordinate humans and AI agents on one branch, decide who owns which files before anyone starts, keep the agent's edits out of the files you have open, commit in small steps so both sides rebase onto real history, and push only with `--force-with-lease` (never plain `--force`). The risk is not that the agent writes bad code; it is that two writers with different views of the same files overwrite each other, and an agent writes much faster than you notice.

## What goes wrong on a shared branch

Three failure modes cover most incidents:

- **Same file, same time.** You are halfway through `checkout.ts` in your editor. The agent edits the same file from its own read. Whichever save lands last wins, and the other change is gone or mangled.
- **Stale reads.** The agent read a file ten minutes ago, you changed it since, and its next edit is planned against the old version. Claude Code does add "a note when a file Claude read earlier changes on disk," which helps, but only for files that agent read, and only on its next step.
- **History rewrites.** Someone rebases the shared branch and force-pushes. A teammate or an agent that built on the old history either loses work or reintroduces it.

## Step 1: split ownership between people and agents

The simplest rule that works: **an agent never edits a file a human has open with unsaved or uncommitted changes, and a human never edits files an agent is actively working on.** Make that concrete:

- **By directory.** "The agent owns `tests/` and `src/api/orders/`; I own `src/web/`." Put it in the prompt and in `CLAUDE.md` or `AGENTS.md` if it holds for the day.
- **By layer.** Humans on the tricky core logic, agents on the mechanical edges (tests, migrations, docs, call-site updates).
- **By time.** You review while the agent works on the next unit, not the same one.

If the agent needs your file, have it stop and ask. A line in the prompt does it:

```text
Only edit files under src/api/orders/ and tests/orders/. If you need to change anything else, stop and tell me which file and why.
```

## Step 2: give agents their own checkout, even on the same branch

You can share a branch without sharing a working directory. Run the agent in a worktree so its edits land on disk somewhere other than the files in your editor:

```bash
git worktree add ../app-agent -b agent/orders-tests
cd ../app-agent && claude
```

Or let Claude Code create one:

```bash
claude --worktree orders-tests
```

Git refuses by default to check out the same branch in two worktrees ("`add` refuses to create a new worktree when `<commit-ish>` is a branch name and is already checked out by another worktree"), so the agent works on its own short-lived branch and you merge or rebase it into the shared one. That keeps the shared branch as the meeting point and your working directory as yours.

If a worktree per task is too heavy (large dependency installs, slow builds), the alternative is a shared checkout with strict file ownership and something that enforces it, covered in the last section.

## Step 3: commit small, rebase often

Small commits are what make a shared branch survivable. Every commit is a point both sides can rebase onto instead of a pile of uncommitted edits that only exist on one disk.

For humans and agents alike:

```bash
git add -p                     # stage only what belongs in this commit
git commit -m "orders: validate quantity before reserve"
git pull --rebase              # replay your commits on top of the shared branch
# run the tests
git push
```

To make every pull rebase instead of merge, set it once: `git config pull.rebase true`. Ask agents to commit at each green test run rather than at the end of the task; it shrinks the window in which their work exists only in one place.

## Step 4: push safely

When anyone (human or agent) must rewrite history on a shared branch, use:

```bash
git push --force-with-lease
```

Git's docs explain why: if somebody else built on the original history while you were rebasing, "blindly pushing with `--force` will lose their work." With `--force-with-lease`, the push succeeds only if the remote is still where you last saw it. Add a rule to your agent instructions:

```markdown
## Git
- Never run `git push --force`. Use `git push --force-with-lease` only when told to rewrite history.
- Never rebase or reset the shared branch `feature/checkout-v2`.
```

## Step 5: make review explicit

On a branch where an agent wrote half the commits, reviewers need to know which half. Three habits:

- **Agent commits are labeled.** Claude Code adds commit attribution lines by default (configurable with the `attribution` setting), so agent-authored commits are recognizable in `git log`.
- **Owners are requested automatically.** A `CODEOWNERS` file means "code owners are automatically requested for review when someone opens a pull request that modifies code that they own." That applies whether a person or an agent made the change.

```text
# .github/CODEOWNERS
/src/billing/   @acme/payments-team
*.sql           @acme/data-team
```

- **Humans own the merge.** Agents can open and update pull requests; a person approves and merges.

## How Cork helps

The steps above depend on everyone remembering the ownership rules, and agents write fast enough that one slip costs real work. Cork enforces it between agents, whoever is running them. Cork is the [coordination layer](https://corkcli.com/blog/coordination-layer-ai-coding-agents) for multiplayer coding agents: it keeps Claude Code, Codex and Cursor agents, on one machine or across a team's machines, from overwriting each other on one codebase, and hands each agent what the others already learned. It coordinates agents and the people running them; keep the ownership rule from step 1 for the edits you make by hand.

For this problem specifically:

- **[Write gate](https://corkcli.com/blog/write-gate):** an edit against code another agent is changing right now is held until your agent has their version, which makes a shared checkout workable without a worktree per task.
- **Live awareness:** when someone else's agent renames a function or changes a signature yours depends on, your agent hears it on its next step instead of planning against a stale read.
- **Landing:** `git push` becomes one step that rebases onto teammates' commits, reruns the tests and pushes.
- **Agent messaging:** any agent can message whoever is working on a given function, so handoffs between a person's agent and a teammate's agent do not go through chat.

Install it once for every repo on the machine:

```bash
curl -fsSL https://corkcli.com/install.sh | sh
```

## Frequently asked questions

### Can I edit code while Claude Code is editing the same project?

Yes, but not the same files at the same time. Split ownership by directory, or run the agent in a separate worktree so its edits do not land in the files your editor has open.

### Should an AI agent push directly to a shared branch?

Only with small commits, a rebase onto the latest branch tip, a passing test run, and never a plain `--force`. Many teams have agents push to their own short-lived branch and let a person merge.

### How do I stop an agent from overwriting my uncommitted changes?

Commit or stash before handing the agent a task in the same checkout, keep the agent out of files you are editing, or give it its own worktree. A write gate holds conflicting edits automatically.

### How do reviewers tell agent commits from human ones?

Claude Code adds attribution lines to commits and pull requests by default. Keep them on for shared branches.

## Sources

- <https://code.claude.com/docs/en/how-claude-code-works>
- <https://code.claude.com/docs/en/worktrees>
- <https://git-scm.com/docs/git-worktree>
- <https://git-scm.com/docs/git-pull>
- <https://git-scm.com/docs/git-push>
- <https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners>

## Related reading

- [How to share context between Codex and Claude Code](https://corkcli.com/blog/share-context-codex-claude-code): Make Codex and Claude Code work from the same context: one AGENTS.md, shared MCP servers, handoff files, and live awareness when both edit the same repo.
- [How to coordinate AI coding agents across a team's machines](https://corkcli.com/blog/coordinate-ai-coding-agents-across-team): Coordinate AI coding agents across a team's machines: shared agent instructions, branch and PR rules, CI gates, live signals, and what built-in tools cover.
- [Parallel AI coding agents on a team: how to split who works on what](https://corkcli.com/blog/split-work-parallel-ai-coding-agents-team): How a team splits work across parallel AI coding agents: slice by code ownership, name shared files, one branch per agent, small tasks, and live coordination.
- [How to land agent changes in one step: rebase, test and push](https://corkcli.com/blog/land-agent-changes-rebase-test-push): Land AI agent commits in one step: fetch, rebase onto teammates' work, rerun tests and push, using a script, a pre-push hook or a merge queue.

[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/humans-and-ai-agents-same-branch
