Jorge Turbica / blog · Blog ← All articles ES · Leer en español
Technologies

Skill of the day: gh-stack, your agent splits work into stacked PRs and keeps them rebased

GitHub's official skill teaches agents to drive gh stack without hanging: one layer per PR, chained rebases, ordered merges.

Nobody really reviews a 2,000-line PR; they approve it. The gh-stack skill, published by GitHub alongside its gh stack extension, teaches your agent to split work into a chain of small PRs, each showing only its own layer's diff, and to keep them rebased so you don't have to. It has 22,900 installs on skills.sh and is currently on the site's Hot list.

What it does

A stack is a linear chain of branches rooted on main: auth is based on main, api on auth and frontend on api. Each branch gets a PR whose base is the branch below, so reviewers see only that layer's changes. Merging goes bottom-up.

The skill triggers when you ask to create, view, sync or merge a stack, mention dependent PRs or layers, or ask to split work for review. From there the agent follows a specific playbook:

  • Create the stack before writing code. It does not build everything on main and slice it later: one concern per layer, bottom to top.
  • To change a lower layer, go there. It runs gh stack down or checkout, edits, commits and runs gh stack rebase --upstack to replay everything above.
  • Always use the non-interactive form. This is the most valuable part of the SKILL.md: gh stack opens a full-screen UI when it detects a terminal, and under an agent's PTY it blocks forever. The skill mandates view --json, submit --auto, merge <n> --yes and init <branch>, and bans modify and switch, which only exist as interactive UIs.
  • Branch on exit codes. The SKILL.md documents codes 0 to 10 with their recovery: 3 is a rebase conflict, 8 means another process holds the stack file lock, and 9 means Stacked PRs are not enabled on the repository, in which case the agent has to tell you.

How to install and use it

First the extension, which needs Git 2.36+ and an authenticated GitHub CLI:

bash
gh extension install github/gh-stack
git config rerere.enabled true        # remember conflict resolutions
git config remote.pushDefault origin  # required with more than one remote

Then the skill, through the GitHub CLI itself or the skills.sh installer:

bash
gh skill install github/gh-stack
# or
npx skills add https://github.com/github/gh-stack --skill gh-stack

The core loop the agent follows is short: gh stack init auth, commits, gh stack add api, more commits, then gh stack submit --auto, which pushes every branch and opens the PRs as drafts (--open makes them ready for review). Once the bottom PR merges, gh stack sync --prune pulls the changes, rebases the rest and deletes local branches that were merged.

Risks and limits

The skill ships no scripts of its own: just instructions and three reference files (stack design, commands and troubleshooting). Everything it runs is git and gh stack under your gh session, so the agent acts with your GitHub permissions. skills.sh lists all three security audits it publishes (Gen Agent Trust Hub, Socket and Snyk) as passing.

What to watch:

  • It rewrites history. rebase and sync rewrite and push branches. The skill never auto-stashes and stops if a worktree is dirty, but on a shared repository you should still review before granting the agent unattended push.
  • gh stack merge --yes merges in a chain. Given a PR number it merges that PR and every unmerged PR below it, all-or-nothing. If your process requires human approval, keep it off the agent's allowed-commands list.
  • Linear stacks only. One parent, at most one child. Parallel work means separate stacks. Reordering or removing layers has no non-interactive path: you unstack and recreate.
  • The repository needs Stacked PRs. Without them, exit code 9 says so and the skill has no fallback.
  • Auto-generated titles. With --auto, PR titles and bodies are generated; use gh pr edit afterwards to change them.

Compared with doing it by hand

Git already covers part of this: git rebase --update-refs force-updates every branch that points at commits being rebased, which handles the most tedious part of keeping stacked branches in shape. What it does not touch is GitHub: retargeting each PR's base when the one below merges, linking them as a stack, or merging them in order. That is what gh stack adds, and the skill contributes what an agent cannot know on its own: which commands hang without a terminal and how to recover from each failure.

If you already manage branches with jj, Sapling or git-town, gh stack link builds the stack on GitHub from branch names or PR numbers without touching your local tool.

Sources

Jorge Turbica

Software Engineer and SRE. I write about the real challenges I run into in production.

See portfolio ↗