Sub topic 2
What a merge queue does
This chapter assumes you know Git branching and merging, GitHub pull requests, and required status checks. You should also have seen branch protection rules or rulesets. The previous chapter showed that a pull request can pass its checks and still break main. This chapter explains how a merge queue closes that gap.
The core idea
A merge queue is an ordered line of pull requests that have been approved and are waiting to merge. Instead of merging your pull request directly into main, you add it to the queue. GitHub then tests the pull request in the state it would have after merging, and merges it only if that test passes.
The state under test is the latest main plus every queued pull request ahead of yours, plus your own changes. Your pull request is therefore never tested against a stale base. A pull request that passed against last week's main must still pass against what main will be when it lands.
The steps a queued pull request goes through
- A pull request meets the branch's requirements, such as approvals and passing checks on the pull request branch.
- A person (or automation) adds it to the queue.
- GitHub creates a temporary branch that combines the current base branch, the entries ahead in the queue, and this pull request.
- The required checks run against that temporary branch.
- If the checks pass, the pull request merges into the base branch. If they fail, it is removed from the queue.
Because each entry builds on the ones ahead of it, the queue behaves like a prediction of what main will look like. GitHub calls the unit it tests a merge group, and the temporary branch carries a gh-readonly-queue/ prefix. A later chapter covers the details of both.
Why this prevents a broken main
Suppose PR A renames a function and PR B adds a new call to the old name. Each passes its own checks, because neither sees the other. Without a queue, both merge and main fails to build. With a queue, PR B is tested on top of PR A. The build fails in the queue, PR B is removed, and main never goes red.
Parallel builds, serial results
Testing one pull request at a time would be slow. The queue builds several temporary branches at once, each assuming the entries ahead of it will merge. Merging still happens in queue order.
If an entry ahead fails and is removed, GitHub discards the temporary branches that depended on it. It rebuilds them without the failed entry. Failures therefore cost extra CI runs, which is why settings such as build concurrency affect cost. Those settings come later in this tutorial.
What the queue does not do
| Task | Handled by the queue? |
|---|---|
| Test against the latest base plus queued changes | Yes |
| Merge in a consistent order | Yes |
| Require review approvals | No, branch protection or rulesets do |
| Fix a failing pull request | No, the author must push a fix and re-queue |
| Run your checks automatically | Only if workflows listen for the `merge_group` event |
The last row is a common mistake. A required check that never runs on the temporary branch never reports a status, and the queue waits on it. The trigger is covered in a later chapter.
Exercise
Pull requests A and B are queued in that order, and both passed their checks before queueing. What is B's temporary branch built from, and what happens to B if A fails in the queue?
Show answer
B's temporary branch is the current base branch plus A plus B. If A fails, A is removed and B's branch is discarded and rebuilt without A, so B is retested on the base branch plus B alone.