{"slug": "merging-many-worktrees-into-one-line", "title": "Merging Many Worktrees Into One Line", "summary": "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.", "body_md": "*A free app I built myself. Give it a try.*\n\nOnce 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.\n\nIn 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.\n\nSo 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.\n\nIt 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.\n\nMerge 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.\n\nMy `--gw1` flag does this. The plan it prints before running looks like this.\n\n```\nmerge plan — linearize 3 worktrees onto 'master', then ff-all:\n  master               pull --ff-only: already up to date\n  master               (base, tip ce5b697f)\n  docs                 ff-advance chain to be09cdb1\n  feature/login        rebase --onto <docs> ce5b697f\n  then fast-forward every branch (incl. master) to the final tip\n  then push master → origin/master (fast-forward, never forced)\n```\n\nThe 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.\n\n```\n* 8494e3f add login page\n* be09cdb write docs\n* ce5b697 init\n```\n\nA 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.\n\n```\ngit-worktree: rebase conflict on 'feature/login' (onto previous tip) — rolled back\nto entry state (except the origin ff of 'master'). resolve 'feature/login' manually,\nthen retry.\n```\n\nBecause 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.\n\n| Stage | On failure | \n|---|---|\n| Syncing the default branch with origin (first) | Touch nothing, abort everything | \n| Rebase chain / ff alignment | Roll every branch back to its entry state | \n| Final push to origin | No rollback. The linearization stands | \n\nThe 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.\n\nWorktree 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.\n\nNext time you run three or more branches at once, write the script that will combine them first. Put the rollback in that script.", "url": "https://wpnews.pro/news/merging-many-worktrees-into-one-line", "canonical_source": "https://dev.to/droidactor/merging-many-worktrees-into-one-line-4lmj", "published_at": "2026-11-19 06:09:47+00:00", "updated_at": "2026-11-19 06:11:44.328827+00:00", "lang": "en", "topics": ["ai-agents", "developer-tools"], "entities": ["Git"], "also_reported_by": [], "takeaway": "Chaining branches with `rebase --onto` in descent order and fast-forwarding all worktrees to one tip yields a linear history where every folder builds the same commit, and rolling each branch back to its entry SHA on conflict makes a dry run unnecessary.", "recommendation": {"advice": "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.", "playbook": "agent-architecture", "playbook_url": "https://wpnews.pro/playbooks/agent-architecture"}, "alternates": {"html": "https://wpnews.pro/news/merging-many-worktrees-into-one-line", "markdown": "https://wpnews.pro/news/merging-many-worktrees-into-one-line.md", "text": "https://wpnews.pro/news/merging-many-worktrees-into-one-line.txt", "jsonld": "https://wpnews.pro/news/merging-many-worktrees-into-one-line.jsonld"}}