title: Claude PR description: Run Claude Code in an isolated worktree and create a PR tags:
- claude
- git
- pr tool: true
Protocol: Claude Headless PR
Run Claude Code headlessly in an isolated git worktree, then create a PR.
Input
- Task: What Claude should do (required). Use what the user says with minimal modification, just send it directly to claude.
- Repo: Path to git repository. Use zorepo1
Instructions for Zo
You will run commands using run_bash_command. Each call is a fresh shell—variables don't persist between calls. Follow these steps, substituting the actual values you receive from command output.
Step 1: Create worktree and capture paths
Run this command. It will output the BRANCH and WORKTREE paths—save these for subsequent steps.
REPO="/home/workspace/Code/zorepo1" BRANCH="zo/$(date +%Y%m%d-%H%M%S)" WORKTREE="/tmp/worktrees/${BRANCH}" cd "$REPO" git fetch origin main --quiet mkdir -p "$(dirname "$WORKTREE")" git worktree add "$WORKTREE" origin/main --quiet cd "$WORKTREE" git checkout -b "$BRANCH" echo "BRANCH=$BRANCH" echo "WORKTREE=$WORKTREE"
Capture the BRANCH and WORKTREE values from stdout. You'll use these exact strings in all subsequent commands.
Step 2: Run Claude headlessly
Run Claude with the user's task. Critical: set cwd to the WORKTREE path from step 1 so Claude operates in the isolated repo.
pwd && claude -p "<TASK>" --output-format json
Replace <TASK> with the user's task description. The pwd confirms the working directory before Claude starts. This may take several minutes.
Step 3: Check for changes and create PR
Use the WORKTREE path as cwd. Check if there are changes, then commit and create PR:
if [ -n "$(git status --porcelain)" ]; then git add . git commit -m "zo: <SHORT_SUMMARY>" git push -u origin "<BRANCH>" gh pr create --title "zo: <SHORT_SUMMARY>" --body "<FULL_TASK>" --base main else echo "NO_CHANGES" fi
Replace:
<BRANCH>with the branch name from step 1<SHORT_SUMMARY>with a brief description of what was done<FULL_TASK>with the original task description
Capture the PR URL from gh output (or note if NO_CHANGES).
Step 4: Cleanup
Always run this, whether previous steps succeeded or failed. Use the REPO as cwd:
git worktree remove "<WORKTREE>" --force
Replace <WORKTREE> with the worktree path from step 1.
Output
Return the PR URL to the user.