Safely merges GitHub pull requests using gh CLI. Use when the user wants to merge a PR, merge a pull request, or complete code review.
Assists with safely merging GitHub pull requests using the gh CLI, following best practices for code review, CI checks, and merge strategies.
IMPORTANT: When using this skill, announce to the user: "Using merge-pr skill to merge a pull request safely."
ALWAYS verify the following before merging:
# View PR details, status checks, and reviews
gh pr view [PR_NUMBER]
# Check CI status
gh pr checks [PR_NUMBER]
# View PR diff (optional, for review)
gh pr diff [PR_NUMBER]
Required conditions before merging:
If checks fail:
gh pr checks --watch [PR_NUMBER]Default: Merge Commit (--merge)
:inbox_tray: Merge pull request #[PR_NUMBER] from [BRANCH_NAME]# Get PR branch name with owner (e.g., "owner/branch-name")
BRANCH_NAME=$(gh pr view [PR_NUMBER] --json headRepositoryOwner,headRefName -q '.headRepositoryOwner.login + "/" + .headRefName')
# Merge with custom subject
gh pr merge [PR_NUMBER] --merge --subject ":inbox_tray: Merge pull request #[PR_NUMBER] from $BRANCH_NAME"
Alternative strategies (use only when appropriate):
Squash Merge (--squash)
gh pr merge [PR_NUMBER] --squash
Rebase Merge (--rebase)
gh pr merge [PR_NUMBER] --rebase
Branch deletion is handled by GitHub repository settings:
--delete-branch flag - respect the repository configurationAfter merging a PR, ALWAYS update your local default branch:
# 1. Get the repository's default branch name
DEFAULT_BRANCH=$(gh repo view --json defaultBranchRef -q .defaultBranchRef.name)
# 2. Switch to the default branch
git checkout "$DEFAULT_BRANCH"
# 3. Pull the latest changes (including the merge)
git pull
# 4. (Optional) Clean up local merged branches
git branch --merged | grep -v "\*" | grep -v "$DEFAULT_BRANCH" | xargs -n 1 git branch -d
Alternative (if you know the default branch name):
# If you know it's 'main'
git checkout main && git pull
# If you know it's 'master'
git checkout master && git pull
Follow this sequence:
# 1. Check PR status and reviews
gh pr view [PR_NUMBER]
# 2. Verify all checks pass
gh pr checks [PR_NUMBER]
# 3. Get PR branch name with owner for merge commit subject
BRANCH_NAME=$(gh pr view [PR_NUMBER] --json headRepositoryOwner,headRefName -q '.headRepositoryOwner.login + "/" + .headRefName')
# 4. Merge with default strategy (merge commit) and custom subject
gh pr merge [PR_NUMBER] --merge --subject ":inbox_tray: Merge pull request #[PR_NUMBER] from $BRANCH_NAME"
# 5. Verify merge completed
gh pr view [PR_NUMBER]
# Should show status: MERGED
# 6. Update local repository
DEFAULT_BRANCH=$(gh repo view --json defaultBranchRef -q .defaultBranchRef.name)
git checkout "$DEFAULT_BRANCH"
git pull
# 7. (Optional) Clean up local merged branches
git branch --merged | grep -v "\*" | grep -v "$DEFAULT_BRANCH" | xargs -n 1 git branch -d
NEVER merge if:
ALWAYS:
--merge (default strategy) to preserve commit history:inbox_tray: Merge pull request #[PR_NUMBER] from [BRANCH_NAME]# Basic merge commit (DEFAULT) - with custom subject
BRANCH_NAME=$(gh pr view [PR_NUMBER] --json headRepositoryOwner,headRefName -q '.headRepositoryOwner.login + "/" + .headRefName')
gh pr merge [PR_NUMBER] --merge --subject ":inbox_tray: Merge pull request #[PR_NUMBER] from $BRANCH_NAME"
# Squash merge (only when needed)
gh pr merge [PR_NUMBER] --squash
# Rebase merge (only when needed)
gh pr merge [PR_NUMBER] --rebase
# Auto-merge (merge when checks pass) - with custom subject
BRANCH_NAME=$(gh pr view [PR_NUMBER] --json headRepositoryOwner,headRefName -q '.headRepositoryOwner.login + "/" + .headRefName')
gh pr merge [PR_NUMBER] --auto --merge --subject ":inbox_tray: Merge pull request #[PR_NUMBER] from $BRANCH_NAME"
# Merge current branch's PR
BRANCH_NAME=$(gh pr view --json headRepositoryOwner,headRefName -q '.headRepositoryOwner.login + "/" + .headRefName')
gh pr merge --merge --subject ":inbox_tray: Merge pull request #[PR_NUMBER] from $BRANCH_NAME"
# Disable auto-merge
gh pr merge [PR_NUMBER] --disable-auto
If you're on a branch with an open PR:
# Get current PR number and branch name
PR_NUMBER=$(gh pr view --json number -q .number)
BRANCH_NAME=$(gh pr view --json headRepositoryOwner,headRefName -q '.headRepositoryOwner.login + "/" + .headRefName')
# Merge the PR for current branch
gh pr merge --merge --subject ":inbox_tray: Merge pull request #$PR_NUMBER from $BRANCH_NAME"
# Check PR status for current branch
gh pr status
# View current branch's PR
gh pr view
# Check PR status
gh pr view 42
# Verify checks pass
gh pr checks 42
# Get branch name and merge with custom subject
BRANCH_NAME=$(gh pr view 42 --json headRepositoryOwner,headRefName -q '.headRepositoryOwner.login + "/" + .headRefName')
gh pr merge 42 --merge --subject ":inbox_tray: Merge pull request #42 from $BRANCH_NAME"
# Update local default branch
DEFAULT_BRANCH=$(gh repo view --json defaultBranchRef -q .defaultBranchRef.name)
git checkout "$DEFAULT_BRANCH"
git pull
gh pr view 42
gh pr checks 42
# Get branch name and merge
BRANCH_NAME=$(gh pr view 42 --json headRepositoryOwner,headRefName -q '.headRepositoryOwner.login + "/" + .headRefName')
gh pr merge 42 --merge --subject ":inbox_tray: Merge pull request #42 from $BRANCH_NAME"
# Update and clean up
DEFAULT_BRANCH=$(gh repo view --json defaultBranchRef -q .defaultBranchRef.name)
git checkout "$DEFAULT_BRANCH"
git pull
git branch --merged | grep -v "\*" | grep -v "$DEFAULT_BRANCH" | xargs -n 1 git branch -d
# Get branch name and set PR to auto-merge when checks pass
BRANCH_NAME=$(gh pr view 99 --json headRepositoryOwner,headRefName -q '.headRepositoryOwner.login + "/" + .headRefName')
gh pr merge 99 --auto --merge --subject ":inbox_tray: Merge pull request #99 from $BRANCH_NAME"
# Wait for merge, then update
gh pr view 99 # Check if merged
DEFAULT_BRANCH=$(gh repo view --json defaultBranchRef -q .defaultBranchRef.name)
git checkout "$DEFAULT_BRANCH"
git pull
# When you're on the PR branch
gh pr view # Verify it's the right PR
# Get PR number and branch name
PR_NUMBER=$(gh pr view --json number -q .number)
BRANCH_NAME=$(gh pr view --json headRepositoryOwner,headRefName -q '.headRepositoryOwner.login + "/" + .headRefName')
gh pr merge --merge --subject ":inbox_tray: Merge pull request #$PR_NUMBER from $BRANCH_NAME"
# Update default branch
DEFAULT_BRANCH=$(gh repo view --json defaultBranchRef -q .defaultBranchRef.name)
git checkout "$DEFAULT_BRANCH"
git pull
gh pr checks passesgh pr viewgh repo view --json defaultBranchRef to get the correct default branch--merge to preserve commit history:inbox_tray: Merge pull request #[PR_NUMBER] from [BRANCH_NAME] format# PR cannot be merged automatically
# You need to resolve conflicts first
# Update branch with base
gh pr checkout [PR_NUMBER]
git fetch origin
DEFAULT_BRANCH=$(gh repo view --json defaultBranchRef -q .defaultBranchRef.name)
git merge "origin/$DEFAULT_BRANCH"
# Resolve conflicts
git push
# Then retry merge with custom subject
BRANCH_NAME=$(gh pr view [PR_NUMBER] --json headRepositoryOwner,headRefName -q '.headRepositoryOwner.login + "/" + .headRefName')
gh pr merge [PR_NUMBER] --merge --subject ":inbox_tray: Merge pull request #[PR_NUMBER] from $BRANCH_NAME"
# Revert the merge commit
DEFAULT_BRANCH=$(gh repo view --json defaultBranchRef -q .defaultBranchRef.name)
git checkout "$DEFAULT_BRANCH"
git pull
git log # Find the merge commit hash
git revert -m 1 [MERGE_COMMIT_HASH]
git push
# Check current branch
git branch
# Ensure you're on the default branch
DEFAULT_BRANCH=$(gh repo view --json defaultBranchRef -q .defaultBranchRef.name)
git checkout "$DEFAULT_BRANCH"
# If you have uncommitted changes
git stash
git pull
git stash pop
# Get default branch name
gh repo view --json defaultBranchRef -q .defaultBranchRef.name
# Get full default branch info
gh repo view --json defaultBranchRef
# View detailed status
gh pr view [PR_NUMBER] --json statusCheckRollup,reviewDecision
# Watch checks until they complete
gh pr checks [PR_NUMBER] --watch
# Use a loop (be careful!)
for pr in 42 43 44; do
if gh pr checks $pr; then
BRANCH_NAME=$(gh pr view $pr --json headRepositoryOwner,headRefName -q '.headRepositoryOwner.login + "/" + .headRefName')
gh pr merge $pr --merge --subject ":inbox_tray: Merge pull request #$pr from $BRANCH_NAME"
fi
done
# Update local once after all merges
DEFAULT_BRANCH=$(gh repo view --json defaultBranchRef -q .defaultBranchRef.name)
git checkout "$DEFAULT_BRANCH"
git pull
# Check if PR can be merged
gh pr view [PR_NUMBER] --json mergeable,mergeStateStatus
Always check if the project has specific merge requirements:
CONTRIBUTING.md for merge strategy preferences--merge unless project specifies otherwisegh repo view --json defaultBranchRef to detect the default branch dynamically:inbox_tray: Merge pull request #[PR_NUMBER] from [BRANCH_NAME]