How to Auto-Merge a Pull Request When Checks Pass
gh pr merge --auto queues the PR to merge itself the moment required checks and reviews are satisfied.
Turn on auto-merge in the repo settings, then call gh pr merge --auto from a workflow (or the UI). GitHub waits for required checks and approvals, then merges with your chosen strategy.
Steps
- Enable Allow auto-merge in repository settings.
- Require the checks you care about in branch protection.
- Call
gh pr merge --autowith a merge strategy flag.
Terminal
Terminal
# Queue this PR to merge once required checks pass
gh pr merge 123 --auto --squash
# From a workflow, target the current PR
gh pr merge "$PR_URL" --auto --merge --delete-branchWorkflow
.github/workflows/ci.yml
on:
pull_request:
types: [labeled]
permissions:
contents: write
pull-requests: write
jobs:
enable-automerge:
if: github.event.label.name == 'automerge'
runs-on: ubuntu-latest
steps:
- run: gh pr merge "${{ github.event.pull_request.number }}" --auto --squash
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}Gotchas
- Auto-merge requires branch protection with at least one required check; without it the flag errors.
- A dismissed review or a new required check re-blocks the queued merge until satisfied again.
Related guides
How to Trigger a Deploy From a PR LabelRun a deployment or preview job only when a specific label is added to a pull request by gating a workflow on…
How to Require Conventional Commit PR TitlesEnforce a Conventional Commits format on pull request titles with amannn/action-semantic-pull-request, failin…