← the whole session plugin/skills/worktree-pr-workflow/SKILL.md

When an agent has completed work, isolate it in a git worktree, commit, push, and open a PR for human review.

Worktree → Branch → Commit → PR Workflow

Purpose: Take your completed work product, isolate it in a worktree, commit it on a clean branch, open a PR, and clean up. The human reviews the PR.

Announce at start: "Using worktree-pr-workflow to isolate my changes and open a PR."

The Workflow

Step 1: Identify Your Changes

Before anything else, capture what you changed:

# From the main checkout
cd <repo-root>
git status
git diff
git diff --staged

Note every modified/added/deleted file. These are your work product.

Step 2: Create a Worktree with a New Branch

# Branch name should describe the work (kebab-case, prefixed)
# Examples: fix/login-redirect, feat/dark-toast-animation, chore/cleanup-dead-routes
git worktree add .worktrees/<branch-name> -b <branch-name>

Branch naming rules:

  • fix/ for bug fixes
  • feat/ for new features
  • chore/ for cleanup/refactoring
  • Keep it short but descriptive

Step 3: Apply Your Changes to the Worktree

Copy your changed files into the worktree. The worktree starts as a clean checkout of the base branch, so you need to bring your work over:

# For each changed file, copy it over
cp <file> .worktrees/<branch-name>/<file>

# For deleted files, remove them in the worktree
rm .worktrees/<branch-name>/<file>

# For new files, copy them over (ensure parent dirs exist)
mkdir -p .worktrees/<branch-name>/<parent-dir>
cp <new-file> .worktrees/<branch-name>/<new-file>

Alternative if many files changed: Use git diff to create a patch and apply it:

git diff > /tmp/agentic-work.patch
cd .worktrees/<branch-name>
git apply /tmp/agentic-work.patch
rm /tmp/agentic-work.patch

Step 4: Commit in the Worktree

cd .worktrees/<branch-name>

# Stage the specific files you changed (NEVER git add -A)
git add <file1> <file2> <file3>

# Commit with a clear message
git commit -m "$(cat <<'EOF'
<type>(<scope>): <short description>

<what changed and why — 1-3 lines>

<attribution line — use whatever attribution convention this session has been given (a Co-Authored-By trailer, or none, per the person's own instructions), never a hardcoded model name that will go stale>
EOF
)"

Step 5: Push and Create PR

# Push the branch
git push -u origin <branch-name>

# Create the PR
gh pr create --title "<short title under 70 chars>" --body "$(cat <<'EOF'
## Summary
- <what this PR does>
- <why it's needed>

## Test plan
- [ ] <how to verify this works>

Generated with [Claude Code](https://claude.com/claude-code)
EOF
)"

Print the PR URL so the human can review it.

Step 6: Clean Up — without touching anyone else's work

Never run git checkout -- ., git reset --hard, git clean, or git stash in the main checkout. Other people and other agents may have uncommitted work in that same folder; those commands would destroy it.

cd <repo-root>

# Remove ONLY your own changes from the main checkout, file by file or hunk by hunk,
# and only after confirming the branch contains them:
git diff <branch-name> -- <each file you changed>   # empty = the branch has exactly this content
git restore --source=HEAD -- <each file you changed, and only if the whole file's change was yours>
# If a file also holds someone else's edits, leave it as it is and tell the person which hunks are yours.

# Remove the worktree (the branch and PR still exist)
git worktree remove .worktrees/<branch-name>

Better still: do the work inside the worktree from the start (Claude Code's own worktree support, or Step 2 before you edit anything), so there is nothing in the shared folder to clean up.

Step 7: Report

Tell the human:

PR ready for review: <PR-URL>
Branch: <branch-name>
Changes: <1-line summary>
Worktree cleaned up.

The human reviews and merges the PR. You're done.

Safety Rules

  • NEVER force push
  • NEVER discard changes you didn't make — no git checkout -- ., reset --hard, clean or stash in the shared folder
  • NEVER merge the PR yourself — the human reviews and merges
  • NEVER use git add -A or git add . — stage specific files only
  • NEVER skip the gitignore check — .worktrees/ must be in .gitignore
  • If gh pr create fails (no GitHub CLI, auth issues), push the branch and give the human the branch name so they can create the PR manually

Quick Reference

Step Command Purpose
1 git status && git diff Know what you changed
2 git worktree add .worktrees/<name> -b <name> Isolated workspace
3 cp or git apply Bring work into worktree
4 git add <files> && git commit Commit on clean branch
5 git push -u && gh pr create PR for human review
6 restore only your own files, then git worktree remove Clean up without discarding others' work
7 Print PR URL Human takes it from here