Create a Pull Request
Invocation: Start only when the user explicitly requests this workflow.
Run gh pr --help on demand if you need flag details.
Draft a concise PR title and description from the diff against a base branch, get the user’s approval, then create or update the PR.
1. Determine the base branch
If the user passed a branch name as an argument (e.g. /gh-create-pr feature-branch), use it as BASE_BRANCH and skip auto-detection. Otherwise detect the repository’s default branch, trying each method until one succeeds:
BASE_BRANCH=$(gh repo view --json defaultBranchRef -q '.defaultBranchRef.name' 2>/dev/null)
[ -n "$BASE_BRANCH" ] || BASE_BRANCH=$(git remote show origin 2>/dev/null | grep "HEAD branch" | cut -d: -f2 | xargs)
[ -n "$BASE_BRANCH" ] || BASE_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')
If every method fails, ask the user which branch to use; never fall back to a guessed main or master. Fetch and capture the verified ref as BASE_REF (preferring origin/$BASE_BRANCH), and stop to ask the user if neither ref exists:
git fetch origin "$BASE_BRANCH" 2>/dev/null || true
BASE_REF=""
if git rev-parse --verify "origin/$BASE_BRANCH" >/dev/null 2>&1; then
BASE_REF="origin/$BASE_BRANCH"
elif git rev-parse --verify "$BASE_BRANCH" >/dev/null 2>&1; then
BASE_REF="$BASE_BRANCH"
fi2. Analyze the changes
Exclude lockfiles from the stat so generated churn does not drown the real changes; dependency manifests such as package.json stay in:
git diff "$BASE_REF"...HEAD --stat -- ":(exclude)*.lock" ":(exclude)package-lock.json" ":(exclude)pnpm-lock.yaml"
git log "$BASE_REF"..HEAD --oneline3. Draft the title and description
Read pr-format.md before drafting, and follow its title format, description template, admonition rules, section rules, and example.
Write the description to a temp file outside the working tree so it never lands in a commit:
PR_BODY="$(mktemp -d)/PR_DESCRIPTION.md"
Before displaying the draft:
- Scan the title, body, quoted snippets, command output, and any attachment links for secrets and private details. Follow
/gh-pr-commentsfor the full checklist when it is installed. - Keep the description free of comment attribution callouts (those belong only on comments) and omit
Created with <model> in <harness>footers from both PR descriptions and comments.
Display the title and description to the user.
4. Get approval
Ask the user to approve as-is, request changes (then re-draft), or reject (stop here). Proceed to step 5 only after explicit approval.
5. Commit, push, and create or update the PR
Re-run the secret and private-detail scan if the user edited the draft, then:
-
Commit pending changes, if any. Stage only the specific files this PR should include:
git add <specific intended files> git commit -m "<generated title>" -
Push the branch:
git push -u origin HEAD -
Create or update the PR. Open a ready (non-draft) PR by default so CI and review bots run immediately; pass
--draftonly when the user asks for one. Check for an existing PR first:gh pr view --json number,url 2>/dev/nullIf a PR exists, update it; otherwise create one, always passing
--baseso it targets the right branch:gh pr edit --title "<generated title>" --body-file "$PR_BODY" gh pr create --base "$BASE_BRANCH" --title "<generated title>" --body-file "$PR_BODY"If
gh pr createorgh pr editfails because the GraphQL rate limit is exhausted, use the REST API instead (checkgh api rate_limit --jq '.resources.graphql', or use/gh-babysit-prwhen installed and authorized):gh api "repos/{owner}/{repo}/pulls" -f title="<generated title>" -f head="$(git branch --show-current)" \ -f base="$BASE_BRANCH" -F body=@"$PR_BODY" --jq '.html_url' gh api -X PATCH "repos/{owner}/{repo}/pulls/<pr>" -f title="<generated title>" -F body=@"$PR_BODY" --jq '.html_url' -
Confirm GitHub returned the PR URL, then delete the temp file with
rm -rf "$(dirname "$PR_BODY")".
Done when the PR URL is confirmed and reported to the user.
If the user also asked to monitor the PR through CI and reviews, continue with /gh-babysit-pr when installed and authorized (otherwise report the PR URL and current check status). Post a follow-up PR comment only when the user’s request calls for one, following /gh-pr-comments.
Adapted from t3dotgg/tstack (MIT).