Create a GitHub pull request following project conventions. Use when the user asks to create a PR, submit changes for review, or open a pull request. Handles commit analysis, branch management, PR template usage, and PR creation using the gh CLI tool.
This skill guides you through creating a well-structured GitHub pull request that follows project conventions and best practices.
Before proceeding, verify the following:
gh CLI is installedgh --version
If not installed, inform the user:
The GitHub CLI (
gh) is required but not installed. Please install it:
- macOS:
brew install gh- Other: https://cli.github.com/
gh auth status
If not authenticated, guide the user to run gh auth login.
git status
If there are uncommitted changes, ask the user whether to:
git branch --show-current
Ensure you're not on main or master. If so, ask the user to create or switch to a feature branch.
git remote show origin | grep "HEAD branch"
This is typically main or master.
git log origin/main..HEAD --oneline --no-decorate
Review these commits to understand:
git diff origin/main..HEAD --stat
This shows which files changed and helps identify the type of change.
Before creating the PR, you need the following information. Check if it can be inferred from:
fix/issue-123, feature/new-login)If any critical information is missing, use ask_followup_question to ask the user:
#123, fixes #123, or closes #123 in commit messagesIf the issue number is not found:
I couldn't find a related issue number in the commit messages or branch name. What GitHub issue does this PR address? (Enter the issue number, e.g., "123" or "N/A" for small fixes)
Before creating the PR, consider these best practices:
Rebase on latest main (if needed):
git fetch origin
git rebase origin/main
Squash if appropriate: If there are many small "WIP" commits, consider interactive rebase:
git rebase -i origin/main
Only suggest this if commits appear messy and the user is comfortable with rebasing.
Ensure all commits are pushed:
git push origin HEAD
If the branch was rebased, you may need:
git push origin HEAD --force-with-lease
IMPORTANT: Read and use the PR template at .github/pull_request_template.md. The PR body format must strictly match the template structure. Do not deviate from the template format.
When filling out the template:
#XXXX with the actual issue number, or keep as #XXXX if no issue exists (for small fixes)Use a temporary file for the PR body to avoid shell escaping issues, newline problems, and other command-line flakiness:
Write the PR body to a temporary file:
/tmp/pr-body.md
Create the PR using the file:
gh pr create --title "PR_TITLE" --body-file /tmp/pr-body.md --base main
Clean up the temporary file:
rm /tmp/pr-body.md
For draft PRs:
gh pr create --title "PR_TITLE" --body-file /tmp/pr-body.md --base main --draft
Why use a file? Passing complex markdown with newlines, special characters, and checkboxes directly via --body is error-prone. The --body-file flag handles all content reliably.
After creating the PR:
gh pr edit --add-reviewer USERNAMEgh pr edit --add-label "bug"No commits ahead of main: The branch has no changes to submit
Branch not pushed: Remote doesn't have the branch
git push -u origin HEADPR already exists: A PR for this branch already exists
gh pr viewMerge conflicts: Branch conflicts with base
Before finalizing, ensure:
gh CLI is installed and authenticated