Give every agent task its own branch
By vibe-coding-notes · 0 stars
Never let an agent work on `main`: one branch per task, a clean tree before it starts, a commit after every working step, and no push without your approval. ### Arguments 1. Agents change many files fast and are sometimes confidently wrong. A commit per step turns a bad session into a cheap `git reset` instead of an afternoon of untangling. 2. A clean starting tree keeps the agent's changes alone in the diff, so review covers only what it did. 3. Worktrees let parallel agents run without overwriting each other. Each gets its own directory and branch while your main checkout stays untouched; `claude --worktree <name>` (or `-w`) creates one under `.claude/worktrees/<name>/`, and cloud agents get their own remote checkout per task. 4. Agent PRs deserve the same gate as a new contributor's. Branch protection on `main` with required reviews and checks catches what the session missed. ### Risks and counterarguments - Worktrees start as fresh checkouts: reinstall dependencies and recopy local config. In Claude Code, a `.worktreeinclude` file (gitignore syntax) copies gitignored files such as `.env`. - A branch can be checked out in only one worktree, and ports, databases and caches are shared, so parallel dev servers can collide. - Many small commits make noisy history. Squash or tidy before merging, keeping the record honest. ### What to do next - Deny or gate `git push --force`, `git reset --hard` and `git clean -fd` in the agent's permission settings. - Review with `git diff --stat` first, then `git diff`. - Keep these recovery commands at hand: | Situation | Command | |-----------|---------| | Discard uncommitted changes | `git restore .` and `git clean -fd` (preview with `git clean -nd`) | | Undo last commit, keep changes | `git reset --soft HEAD~1` | | Return to a known good commit | `git reset --hard <sha>` (only on your own unpublished branch) | | Find a "lost" commit | `git reflog` | | Find the breaking commit | `git bisect start`, `git bisect bad`, `git bisect good <sha>` | - Parallel work: ~~~ git worktree add ../myapp-feature-a -b feature-a git worktree list git worktree remove ../myapp-feature-a ~~~ - Add `.claude/worktrees/` to `.gitignore` and remove finished worktrees. - Ask for a PR description with what changed, why, and how it was tested. ### Sources - https://code.claude.com/docs/en/worktrees - https://git-scm.com/docs/git-worktree