Skip to content
Back to skills

Split Pr

ASecurity

Use when the user wants to split a large PR or branch into smaller stacked PRs - "split this PR", "break up this branch", "this PR is too big", "make these stacked PRs", "separate the refactor from the feature", or asks to turn unrelated changes in one branch into a stack of reviewable layers.

  • 2 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added November 15, 2026
ai-agentsrustgobashgitapi

Works with

  • api

Security analysis

A100/100

Scanned November 15, 2026

npx -y skills add XingLiu0923/pr-stack-skill --skill split-pr --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Split Pr?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Split Pr
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/xingliu0923-split-pr/badge)](https://www.skillsdirectory.com/skills/xingliu0923-split-pr)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: split-pr
description: Use when the user wants to split a large PR or branch into smaller stacked PRs - "split this PR", "break up this branch", "this PR is too big", "make these stacked PRs", "separate the refactor from the feature", or asks to turn unrelated changes in one branch into a stack of reviewable layers.
---

# Splitting an existing branch into a stack

**Load `pr-stack:stack` first.** The preflight, the blocked-command contract,
and the non-interactive command forms all apply here.

GitHub's own guidance is to design the layers *before* writing code, and it is
right. But people arrive with the branch already written, and "you should not
have done that" is not a workflow. This skill is that workflow.

## Step 1 — Ask how the user wants to work

Before touching anything, ask exactly one question:

> Do you want to **confirm each layer before I create it**, or should I **split
> automatically and report when I'm done**?

Record the answer and honour it for the whole session. In *confirm* mode, show
the proposed branch name, its file list, and its one-sentence rationale, and wait
for a yes before each command that changes the repository.

Approval is still required in *automatic* mode for anything destructive:
`gh stack unstack`, branch deletion, `git reset --hard`, any force operation.

## Step 2 — Preflight

```bash
bash "${CLAUDE_PLUGIN_ROOT}/scripts/check-status.sh"
git status --porcelain
```

All of these must hold:

* `ext=installed`, `auth=authenticated`, `stacks=available`
* `git status --porcelain` is empty (clean working tree)

If the tree is dirty, stop and ask the user to commit or stash. Never stash on
their behalf. If `stacks=NOT available`, stop — this repository cannot host a
stack, and no amount of splitting will change that.

## Step 3 — Record a safety anchor

```bash
git rev-parse HEAD
```

Report the SHA to the user in plain text: *"Your original branch tip is
`<sha>`."*

This anchor is the recovery path **and** the correctness oracle for step 7. It
does not license `git reset --hard`, which stays approval-gated.

## Step 4 — Analyse the real diff

```bash
TRUNK="$(gh repo view --json defaultBranchRef -q .defaultBranchRef.name)"
git diff --stat "$TRUNK...HEAD"
git diff --name-only "$TRUNK...HEAD"
git diff "$TRUNK...HEAD"
```

Resolve trunk dynamically — never assume `main` or `master`. If a stack already
exists, `gh stack view --json` reports `.trunk` authoritatively.

**Do not trust commit messages.** A commit titled "fix typo" routinely contains
an API change. The diff is the only evidence. Read it.

## Step 5 — Decide the layer order, completely, before creating anything

This is the step that cannot be redone cheaply: **`gh stack` has no
non-interactive reorder**, and `gh stack modify` is TUI-only. Getting the order
wrong costs `gh stack unstack --local` and a rebuild from scratch.

Each layer must be:

* **independent** — it builds and its tests pass on its own,
* **understandable** — a reviewer can state its purpose in one sentence,
* **ordered** — everything it depends on is in a lower layer.

Good seams, in dependency order (bottom first):

1. pure refactors and moves with no behaviour change,
2. new shared helpers, types, schemas, migrations,
3. the core behaviour change,
4. call-site updates and integrations,
5. tests, docs, config, formatting.

Never split a hard dependency: if layer B does not compile without layer A's
change, A must be below B, or they are one layer.

**Stacks are strictly linear.** If the branch contains two genuinely independent
pieces of work, that is *two stacks*, not one branching tree. Say so rather than
forcing a false order.

Name layers `<topic>/<concern>` — `billing/schema`, `billing/api`,
`billing/ui` — unless the repository has its own convention, which wins.

## Step 6 — Execute the split

The whole split runs off the anchor. Bring the final content into the working
tree once, unstaged, then commit it out one layer at a time. `gh stack add` does
not touch the working tree, so the remainder carries forward automatically.

```bash
git checkout "$TRUNK"
gh stack init <bottom-layer>          # creates the stack and checks out layer 1

git checkout <anchor> -- .            # every final file, staged
git restore --staged .                # unstage; the worktree now holds the goal state

git add <files for layer 1>           # or: git add -p <file>  for part of a file
git commit -m "<layer 1 message>"

gh stack add <layer 2>                # uncommitted remainder carries over
git add <files for layer 2>
git commit -m "<layer 2 message>"

# …one `gh stack add` per layer, bottom to top

gh stack add <top-layer>
git add -A                            # everything left, including deletions
git commit -m "<top layer message>"
```

Two details that matter:

* `git add -p` is how a single file gets split across layers. Stage only that
  layer's hunks; the rest stays in the worktree for a later layer.
* The **final** layer must use `git add -A`, not a file list. That is what makes
  step 7 come out empty — it sweeps up anything unaccounted for, including
  deleted files.

## Step 7 — Prove content equivalence (hard gate)

```bash
git diff <anchor> HEAD                # MUST be empty
git diff --stat "$TRUNK...HEAD"       # compare with the step-4 output
```

`git diff <anchor> HEAD` being empty is the proof that the stack contains
exactly what the original branch contained — nothing lost, nothing invented.

**If it is not empty, stop and report.** Show the user the residual diff and ask
how to proceed. Do not invent a commit to absorb it, do not amend a layer to
make the check pass, and do not carry on to step 8. A split that silently drops
a hunk is worse than no split.

## Step 8 — Validate every layer independently

Discover what the repository actually uses — `package.json` scripts, `Makefile`,
`pyproject.toml`, the CI workflow files — or use the configured overrides
`PR_STACK_BUILD_CMD`, `PR_STACK_TEST_CMD`, `PR_STACK_LINT_CMD`. Do not assume
a build system.

Walk bottom to top:

```bash
gh stack bottom
<build> && <test> && <lint>
gh stack up
<build> && <test> && <lint>
# …to the top
```

Every layer must pass on its own. A layer that only passes because of a change
in a higher layer is mis-ordered: check out the lower layer, commit the change
there, then `gh stack rebase --upstack`.

## Step 9 — Submit and report

```bash
gh stack submit --auto            # drafts; add --open for ready-for-review
```

PR titles and bodies are auto-generated. Offer to write real descriptions with
`gh pr edit <number> --body ...` — for a stack, a one-line "what this layer does
and why it is separate" is worth more than a template.

Then report:

* the final `gh stack view --short`,
* layer → purpose → files, bottom to top,
* build/test/lint result per layer,
* **the equivalence check result**,
* the original safety-anchor SHA,
* anything you chose not to split, and why.

## Approval checklist

Never run these without explicit user confirmation, in any mode:

`gh stack unstack` · `gh stack merge` · branch deletion or rename ·
`git reset --hard` · any force operation · discarding the safety anchor.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.