cd /news/ai-agents/merging-many-worktrees-into-one-line · home › topics › ai-agents › article
[ARTICLE · art-172369] src=dev.to ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

Merging Many Worktrees Into One Line

A developer built a Git tool that treats worktree folders as parallel work slots for running multiple AI agents at once, then linearizes their branches into a single history using a chained `rebase --onto` with fast-forwarding. The tool's `--gw1` flag prints a merge plan before executing, and on a rebase conflict it rolls every branch back to its entry SHA rather than leaving half-rewritten history; the developer verified this by running two deliberately conflicting branches. A failed final push is not rolled back, so a linearized result survives network or permission errors and only the push is retried.

recommendation

Give each parallel agent its own worktree folder to avoid file-lock and output-path collisions, then linearize their branches with a rebase chain that rolls back to entry SHAs on conflict.

more in playbook: Designing agent systems
by read4 min views4 publishedNov 19, 2026

A free app I built myself. Give it a try.

Once you have more than three worktrees open, the next problem arrives. How do you put them back together? I seat one worker in each folder and merge everything into one line with a single script at the end. What matters is not the merge strategy but whether a failure lands you back where you started.

In the first post a worktree was a tool for removing the cost of switching branches. The property that each folder is an independent build tree has a second use. Several folders can run at the same time without interfering with each other. Two processes building the same repository collide over output paths and break. With worktrees those paths are different to begin with.

So I use folders as work slots. A documentation worker sits in the docs folder and a feature worker in feature-login. Each runs builds and tests in their own place. The human only reviews, from the master folder.

It suits running several AI agents in parallel especially well. The agents never fight over file locks. When one breaks the build, the other slots are fine.

Merge five branches into master one at a time and the graph turns into a bundle of diamonds. Read that history later and you cannot tell which change was verified on top of which. So I go the other way. The branches go in order of descent. Each is appended onto the new tip of the previous one with rebase --onto. When that is done, every branch is fast-forwarded to the final tip.

My --gw1 flag does this. The plan it prints before running looks like this.

merge plan — linearize 3 worktrees onto 'master', then ff-all:
  master               pull --ff-only: already up to date
  master               (base, tip ce5b697f)
  docs                 ff-advance chain to be09cdb1
  feature/login        rebase --onto <docs> ce5b697f
  then fast-forward every branch (incl. master) to the final tip
  then push master → origin/master (fast-forward, never forced)

The result is one straight line with no merge commits. Every worktree points at the same commit, so a build in any folder gives the same result.

* 8494e3f add login page
* be09cdb write docs
* ce5b697 init

A rebase chain that hits a conflict halfway stops with half of the history rewritten. That state is the biggest risk in the whole thing. So on failure every branch is put back to the sha it had on entry. I built two conflicting branches and actually ran it. All that was left was the line below and exit 1. The shas of all three branches were identical to what they had been.

git-worktree: rebase conflict on 'feature/login' (onto previous tip) — rolled back
to entry state (except the origin ff of 'master'). resolve 'feature/login' manually,
then retry.

Because the rollback is guaranteed, I do not guess at the outcome with a dry run. I just run it and see. A three-way conflict only shows up when a rebase is actually attempted, so this is the faster route. How each stage handles failure is decided in advance.

Stage On failure
Syncing the default branch with origin (first) Touch nothing, abort everything
Rebase chain / ff alignment Roll every branch back to its entry state
Final push to origin No rollback. The linearization stands

The sync goes first because stacking on a stale base means diverging again at push time. At the other end, rolling back a failed push would throw away a linear result you worked for. A network or permission problem should not cost you that. So there the tool leaves git's own refusal message and a retry command. Failure is signalled through the exit code alone. Fix the cause, run it again, and only the push is retried.

Worktree folders are parallel work slots, and the price of adding slots is paid at the final merge. Pay that price as one rebase chain rather than a bundle of merge commits. The history stays readable, and every folder starts again from the same commit. Whether a job can be handed to automation is decided by atomicity on failure, not by speed.

Next time you run three or more branches at once, write the script that will combine them first. Put the rollback in that script.

── more in #ai-agents 4 stories · sorted by recency
── more on @git 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/merging-many-worktre…] indexed:0 read:4min 2026-11-19 · —