Sub topic 17
Auto-merge and merge queues
This chapter assumes you know how pull requests, required status checks, and branch protection rules work, and that you have met the three merge strategies. Here you will see how GitHub can merge a pull request for you once its requirements are met, and how a merge queue keeps a busy main branch healthy.
Auto-merge
Auto-merge is a setting on a single pull request. You turn it on, pick a merge method, and GitHub merges the pull request as soon as every requirement is satisfied. Those requirements are the required reviews and the required status checks from your branch protection rules.
Two conditions apply:
- The repository owner must enable "Allow auto-merge" in the repository settings.
- The target branch must have at least one protection rule that requires a review or a status check. Without a rule, the pull request is already mergeable, so GitHub merges it immediately instead of waiting.
From the command line you can enable it like this:
gh pr merge 123 --auto --squash --delete-branchThe command does not merge right away. It records your choice. When the last required check passes and the last required approval arrives, GitHub performs a squash merge and deletes the branch.
Auto-merge turns itself off in some cases. Pushing new commits by someone without write access disables it. A change that makes the pull request unmergeable, such as a new merge conflict, also stops it. You can also cancel it yourself:
gh pr merge 123 --disable-autoWhy auto-merge helps
Without it, someone has to come back to the pull request after CI finishes and press the button. With auto-merge, the author can request a review, enable auto-merge, and move on. The reviewer's approval is what triggers the merge.
The problem merge queues solve
Suppose two pull requests each pass CI against the current main. Each is correct alone, but together they break the build. The first merges. The second was tested against the old main, so its green check no longer describes what main will contain after it merges.
You can require branches to be up to date before merging. That forces every author to update and re-run CI after each merge, which is slow when many people merge in a day.
Merge queues
A merge queue handles this ordering for you. When a pull request is ready, you add it to the queue instead of merging it. GitHub then does the following:
- Creates a temporary branch that contains the latest
main, any pull requests ahead in the queue, and your changes. - Runs your required checks on that temporary branch.
- Merges the pull request only if the checks pass.
- Removes the pull request from the queue and reports a failure if the checks fail. The pull requests behind it are re-tested without it.
Merge queues are enabled in a branch protection rule or ruleset for the target branch.
Your CI workflow must also run for queue branches. Add the merge_group event, or the queue will wait for checks that never start:
name: CI
on:
pull_request:
branches: [main]
merge_group:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm testChoosing between them
| Situation | Choice |
|---|---|
| Small team, few merges per day | Auto-merge |
| Many contributors, frequent merges, build must stay green | Merge queue |
Exercise
Why does a pull request that passed CI sometimes still break main after merging, and how does a merge queue prevent it?
Show answer
CI ran against the old main, not against main plus other pull requests merged since. A merge queue tests each pull request on a temporary branch that includes the latest main and the queued pull requests ahead of it, and merges only if the checks pass on that combination.
Exercise
You enabled auto-merge on a pull request, but it merged immediately with no waiting. What is the likely cause?
Show answer
The target branch had no protection rule requiring a review or status check, so the pull request was already mergeable.