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.
Safely merges GitHub pull requests using the gh CLI.
Announce when using this skill: "Using merge-pr skill to merge a pull request safely."
gh pr view [PR]
gh pr checks [PR] # add --watch to wait for CI to finish
Confirm before merging:
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"
--merge (preserves full commit history). Use --squash only when the PR has many small/WIP commits to consolidate, --rebase only for small PRs where a linear history without a merge commit is wanted.--subject in the format :inbox_tray: Merge pull request #[PR] from [BRANCH_NAME].--delete-branch — branch deletion is controlled by the repository's "Automatically delete head branches" setting, not by this skill.[PR] in any command above to target the PR of the current branch.--auto and run step 3 once gh pr view [PR] shows MERGED.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 # optional: prune merged local branches
Merge conflicts — resolve on the PR branch, then retry step 2:
gh pr checkout [PR]
git fetch origin
DEFAULT_BRANCH=$(gh repo view --json defaultBranchRef -q .defaultBranchRef.name)
git merge "origin/$DEFAULT_BRANCH"
# resolve conflicts, then:
git push
Merged the wrong PR:
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
Before defaulting to --merge, check CONTRIBUTING.md and the repository's branch protection rules for merge-strategy overrides.