Create a GitHub PR from the current branch - analyze changes, check size limits, and generate PR with proper formatting
Automate the workflow for creating a GitHub PR from the current branch. Performs Issue identification, PR size checking, diff analysis, and PR creation in a single flow.
git branch --show-current
main → display "Error: Cannot create a PR on the main branch. Please switch to a working branch." and exitgh pr list --head <branch name> --json number,url,state
OPEN PR exists → display "Error: This branch already has an open PR: <URL>" and exitgit status --porcelain
git add <file path>, generate an appropriate commit message and git commit (do not use git add -A or ; for untracked files, judge relevance to the changes and exclude unrelated ones)git add .git log main..HEAD --oneline
git push -u origin <branch name>Search for the Issue automatically in the following order (do not ask the user):
git log main..HEAD --format=%s%n%b and look for Issue numbers using the #(\d+) patterngh issue list --search "<keyword>" --json number,title,state with numbers or keywords from the branch namegh issue list --state open --limit 10 --json number,title,labels and infer the Issue from relevance to the branch name and changesgh issue view <number> --json number,title,state and display the title. If verification fails (Issue doesn't exist or is closed), continue without an Issuegit diff --numstat main...HEADUnderstand the changes with git log main..HEAD --oneline and git diff main...HEAD --stat
If needed, review detailed diffs with git diff main...HEAD
Infer the PR type from the branch prefix:
| Prefix | PR Title Prefix |
|---|---|
bugfix/ | fix: |
feature/ | feat: |
enhance/ | enhance: |
docs/ | docs: |
chore/ | chore: |
From the diff file list in git diff main...HEAD --name-only, detect changes that may require documentation updates using the following generic heuristics:
Detection patterns:
page.tsx, page.jsx, page.ts, page.js, route.tsx, route.ts (extract new files only with git diff main...HEAD --diff-filter=A --name-only).claude/skills/*.config.*, .*rc, .*rc.*, tsconfig*.json, scripts section in package.json, etc.)Consistency check targets: When detected, verify the existence and content consistency of:
README.md — whether new features, routes, or config changes are reflected.claude/skills/README.md — whether the skill list is updated when skills are added.claude/rules/ directory — whether rule-related changes are reflectedHandling based on results:
Inconsistency detected: Display a warning in the following format and continue (do not block PR creation):
⚠️ Documentation Consistency Check:
- <detection>: <target document> may need to be updated
No detection patterns matched: Proceed to Step 6 without displaying anything
Generate a PR title (70 characters or less, using the prefix inferred in Step 4)
Generate the PR body using the following template:
## Summary
- <key change 1>
- <key change 2>
Closes #XX ← only when Issue is identified
## Test plan
- [ ] <test item>
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Create the PR with gh pr create --base main --title "..." --body "..."
Use a heredoc for the body to preserve formatting:
gh pr create --base main --title "<title>" --body "$(cat <<'EOF'
<body>
EOF
)"
If it fails, display the error message and exit
Display the creation results in the following format:
## PR Created
PR #XX: <title>
<PR URL>
- Related Issue: #XX <Issue title> ← only when Issue is identified
- Files changed: X
- Lines changed: +XX / -XX