Open a pull request for the current branch. Base the title and body strictly on the branch's real commits and diff — never describe a change that is not in the diff.
Parse $ARGUMENTS
- The exact token
draft(case-insensitive) requests a draft PR — it is never a base branch. Consume it first and remove it from the argument list. - After removing
draft, any remaining token is a requested base branch: accept it only ifgit rev-parse --verify <token>succeeds; otherwise warn and fall back to auto-detection.
Steps
- Detect the remote: take the current branch's upstream remote from
git rev-parse --abbrev-ref @{u}(the part before/); else ifgit remotelists exactly one, use it; else useorigin. Call it<remote>and use it in every command below. - Detect the base branch (unless $ARGUMENTS supplied one): run
git symbolic-ref --short refs/remotes/<remote>/HEAD— it returns<remote>/<name>, so strip the leading<remote>/to get the bare<base>. If that fails, use whichever ofmain/mastergit rev-parse --verifyfinds. - Confirm you are not on the base branch and the branch is ahead:
git log --oneline <base>..HEAD. If zero commits ahead, stop and say so. - Read what changed:
git log <base>..HEADfor the messages,git diff <base>...HEAD --stat(three dots = diff from the merge-base), then the fullgit diff <base>...HEAD. Read enough to state every meaningful change precisely. - Check the working tree with
git status. If uncommitted changes belong in this PR, tell the user and ask whether to commit them first — never commit silently. - Push:
git push -u <remote> HEAD. If the upstream branch has diverged, report it rather than force-pushing. - Check for an existing PR with
gh pr view --json url,state,isDraft. If one is open, update it in place (see Create) instead of opening a duplicate.
PR content
- Title: one imperative line, <=70 chars, stating the net change (not "fix stuff" — name it). Follow the repo's log style; use a Conventional Commits prefix if the history does.
- Body sections:
- Context — the problem or motivation. Add
Closes #Nwhen a commit or the branch name references an issue. - What changed — bullets grouped by area, each tied to the diff. Call out user-facing, API, schema, or config/behavior changes explicitly.
- How tested — the exact commands you ran and their outcome. If you ran none (this command does not run tests), do not just write "not tested": state whether the diff adds or updates tests, and note that CI will verify — after creation, report
gh pr checksstatus if available. Never claim a test passed that you did not run.
- Context — the problem or motivation. Add
- If
.github/PULL_REQUEST_TEMPLATE.md(orpull_request_template.md) exists, fill that template instead of inventing your own structure.
Create
- Write the body to a temp file and pass it via
--body-file(or a heredoc) to preserve Markdown; never inline multi-line Markdown in--body. - New PR:
gh pr create --base <base> --head <branch> --title "…" --body-file <file>. Add--draftwhen $ARGUMENTS requested draft. - Existing open PR (from step 7): update it instead —
gh pr edit --title "…" --body-file <file>. Do not create a second PR. - Print the resulting PR URL, then run
gh pr checksand report the CI status if it returns.
If gh is unavailable
Probe gh --version first. If gh is missing or unauthenticated, still push, then print the title and full Markdown body in one copy-paste block plus the compare URL (https://<host>/<owner>/<repo>/compare/<base>...<branch>), deriving <host>/<owner>/<repo> from git remote get-url <remote>.
Rules
- Never invent, inflate, or drop changes — the body must match the diff exactly.
- Never claim tests passed unless you actually ran them in this session.
- Never force-push, amend, rebase, or edit source files; this command only pushes and opens or edits the PR.
- Keep the body skimmable — no filler, no restating the diff line by line.