Sub topic 13
A script to set up and tear down the workflow
This chapter assumes you have read the earlier chapters. You should know git worktree add, remove and prune, the one-branch-per-worktree rule, and how to start an agent in a background subshell. Here you combine those commands into one script, so the whole lifecycle is repeatable.
Why script it
Typed by hand, the workflow invites small mistakes. You might forget to copy .env, base a branch on the wrong commit, or leave a stale worktree behind. A script gives every run the same layout and the same cleanup. It also makes the task list a single array that you can edit.
Review stays manual on purpose. The script never merges for you.
The script
Save this as agents.sh in the main checkout and make it executable. The task names, the base branch and the sibling-directory layout match the earlier chapters.
#!/usr/bin/env bash
set -euo pipefail
repo=$(git rev-parse --show-toplevel)
name=$(basename "$repo")
base=main
tasks=(auth billing search)
dir_for() { echo "$repo/../$name-$1"; }
setup() {
for task in "${tasks[@]}"; do
git worktree add -b "agent/$task" "$(dir_for "$task")" "$base"
if [ -f "$repo/.env" ]; then
cp "$repo/.env" "$(dir_for "$task")/.env"
fi
done
git worktree list
}
run() {
: "${AGENT_CMD:?set AGENT_CMD to your agent command}"
pids=()
for task in "${tasks[@]}"; do
(
cd "$(dir_for "$task")"
$AGENT_CMD "Work on the $task task described in TASKS.md"
) &
pids+=($!)
done
status=0
for pid in "${pids[@]}"; do
wait "$pid" || status=1
done
return "$status"
}
teardown() {
for task in "${tasks[@]}"; do
git worktree remove "$(dir_for "$task")"
git branch -d "agent/$task"
done
git worktree prune
}
case "${1:-}" in
setup) setup ;;
run) run ;;
teardown) teardown ;;
*) echo "usage: $0 setup|run|teardown" >&2; exit 1 ;;
esacUse it like this:
chmod +x agents.sh
./agents.sh setup
AGENT_CMD="your-agent-command" ./agents.sh run
git diff main...agent/auth
./agents.sh teardownHow the pieces work
set -euo pipefailstops the script at the first failed command, on unset variables, and on failures inside pipes.dir_forkeeps the path convention in one place, so setup, run and teardown cannot disagree.runrecords each background process ID and waits for each one separately. A plainwaitwould hide a failing agent, because it returns zero. Waiting on each ID makesrunreturn non-zero if any agent failed.teardownrelies on Git's own safety checks.git worktree removerefuses to delete a worktree with uncommitted changes.git branch -drefuses to delete a branch that is not merged. Withset -e, either refusal stops the script, which is the behavior you want.
Common errors
- Running from inside a linked worktree.
git rev-parse --show-toplevelthen returns that worktree, and the sibling paths come out wrong. Run the script from the main checkout. - Re-running
setupafter a partial failure.git worktree addfails if the branch or directory already exists. Rungit worktree listto see what exists. Remove the leftovers, then try again. - Teardown before merging.
git branch -dfails for unmerged branches. Merge the branch first. If you decide to discard the work, delete the branch withgit branch -Dyourself, not from the script. - An agent command containing quotes.
$AGENT_CMDis split on whitespace without parsing quotes. If your command needs complex arguments, wrap it in a small executable script and pointAGENT_CMDat that.
Exercise
Why does the run function wait on each process ID instead of calling wait once with no arguments?
Show answer
A bare wait returns zero no matter how the background jobs ended, so a failed agent would go unnoticed. Waiting on each ID lets the script set status=1 when any agent exits with an error, and run then returns a non-zero status.
Exercise
Extend teardown so that a missing worktree directory (for example, one deleted by hand) does not stop the script. Which command already handles that case?
Show answer
Run git worktree prune before the loop, so Git forgets worktrees whose directories no longer exist. The loop will then fail only for worktrees that still exist, or you can guard each removal with [ -d "$(dir_for "$task")" ].