/engineering:topr
/topr — Rebase a Stacked PR onto origin/main
Rebase a stacked PR onto the latest origin/main, correctly dropping commits already merged via squash-and-merge. Use when user wants to refresh a stacked PR after an earlier PR in the stack merged, or says "rebase this PR on main".
<PR number> Checks out a PR branch, rebases it onto the latest `origin/main` (dropping already-merged commits from earlier PRs in the stack), and force-pushes.
Full skill instructions
This is the same file the agent loads when the skill triggers. Source: plugins/engineering/skills/topr/SKILL.md.
This repo uses squash-and-merge. A naive git rebase origin/main will NOT drop already-merged commits because squash-merge creates different SHAs on main. Instead, we must identify the commits unique to this PR and cherry-pick only those onto origin/main.
Instructions
When the user runs /topr <PR>, execute the following steps:
Get PR metadata:
gh pr view <PR> --json headRefName,title,state,commits --jq '.'If the PR is not open, warn the user and stop. Note the number of commits reported by the PR — this is the count of commits unique to this PR.
Fetch latest main and the PR branch:
git fetch origin main <branch>Check out the branch:
git checkout <branch>Identify commits unique to this PR:
This is the critical step. Since we squash-merge,
origin/maincontains squashed versions of earlier PRs in the stack, but git doesn't know they're the same patches. We need to figure out which commits on the branch are actually new in this PR vs carried over from earlier (already-merged) PRs.Strategy: Use
git diffto find the unique commits.a. First, show all commits above main:
git log --oneline origin/main..HEADb. Compare the tree diff against main to understand what this PR actually changes:
git diff --stat origin/main..HEADc. Look at the PR's commit count from step 1. The PR's own commits are the most recent N commits (where N = PR commit count). The older commits below them are from earlier PRs in the stack.
d. If the PR commit count is not available or ambiguous, identify unique commits by checking which files each commit touches vs what's already on main. Commits whose changes are already present on main (via squash-merge) are stale.
e. List the unique commits (most recent N):
git log --oneline -N HEAD # where N is the PR's commit countReset to origin/main and cherry-pick unique commits:
git reset --hard origin/main git cherry-pick <commit1> <commit2> ... # in chronological order (oldest first)- If cherry-pick has conflicts, stop and show them. Ask the user how to proceed.
- Do NOT use
git rebase origin/main— it replays all commits and won't drop squash-merged duplicates.
Verify the result:
git log --oneline origin/main..HEAD git diff --stat origin/main..HEADConfirm that:
- Only the PR's own commits remain (no stale commits from earlier PRs)
- The diff matches what the PR is supposed to change
Force-push:
git push --force-with-lease origin <branch>Report the result:
- Confirm the PR is rebased and pushed.
- Show the final
git log --oneline origin/main..HEAD. - Show how many commits were kept vs dropped.