Sub topic 2
What a merge queue does
This chapter explains how a merge queue behaves from the moment a pull request enters it to the moment its changes reach the base branch. It assumes you know basic Git branching and merging, how pull requests and code review work on GitHub, how branch protection rules or rulesets require status checks, and the syntax of a GitHub Actions workflow. Later chapters cover setup and workflow configuration in detail. Here the goal is a working mental model.
The core idea
A merge queue is an ordered line of pull requests that have been approved and are waiting to merge into a protected branch. Instead of merging a pull request directly, you ask GitHub to add it to the queue. GitHub then does three things:
- It builds a temporary commit that combines the latest base branch, any changes queued ahead of your pull request, and your pull request.
- It runs the required status checks against that temporary commit.
- If the checks pass, it updates the base branch to include the result. If they fail, it removes your pull request from the queue.
The important change is where the testing happens. Without a queue, the checks that gate a merge ran against your pull request branch, possibly combined with an older version of main. With a queue, the checks that gate the merge run against the code that will actually exist on main once the merge completes.
The queue also serializes merges. You do not have to click "Update branch" and wait for CI again every time someone else merges first. The queue takes over that bookkeeping.
What the queue guarantees
If the queue is configured correctly, every commit that lands on the base branch through the queue has been tested in exactly the state in which it landed. The result of the check run is a statement about the real future main, not about a snapshot from hours ago.
This holds because the queue builds each test candidate on top of everything that will be merged before it. Pull request C is tested with the base branch, A, and B already in place, even though A and B have not merged yet. If A and B both pass and merge, then C's test result still describes reality. This is sometimes called speculative testing: the queue assumes the entries ahead will succeed and tests on that assumption.
What the queue does not guarantee
The queue is only as good as the checks it runs. Three limits are worth stating plainly:
- It does not fix flaky tests. A flaky failure removes a pull request from the queue just as a real one does.
- It does not replace code review. Required reviews still apply before a pull request can be added.
- It does not make slow CI fast. It can make slow CI cost more, because it runs checks on merge candidates in addition to the checks on the pull request branch.
The life of a queued pull request
A pull request moves through a small set of states. Knowing them makes the queue's behavior predictable.
| State | Meaning |
|---|---|
| Not queued | The pull request is open and may be passing its own checks. |
| Awaiting checks | It is in the queue, and a temporary branch exists for it. Required checks are running on that branch. |
| Ready to merge | Its checks passed, and every entry ahead of it is also passing or already merged. |
| Merged | The base branch now contains its changes. |
| Removed | A check failed or timed out, or someone removed the pull request. It is out of the queue. |
Entries wait in first-in, first-out order. Position matters because each entry's test candidate includes all the entries ahead of it.
Following one pull request through the queue
The diagram shows two pull requests, A and B, entering the queue in that order. GitHub tests B on top of A without waiting for A to merge.
Without speculative testing, B would wait for A to merge and then wait again for its own checks. The queue overlaps that work, so latency grows more slowly than the number of queued pull requests.
Merge groups and temporary branches
The unit of work the queue tests is a merge group. A merge group is a temporary commit, on a temporary branch, that represents what the base branch would look like if the entries up to and including this one were merged. GitHub creates these branches under a gh-readonly-queue/ prefix, in names of the form gh-readonly-queue/main/pr-<number>-<sha>.
Several things follow from this design:
- Checks do not run on your pull request branch during queue testing. They run on the temporary branch, so a workflow must be set up to respond to that event. A later chapter covers the
merge_grouptrigger. - Temporary branches are created and deleted by GitHub. You should not push to them or build tooling that depends on them lasting.
- The commit that checks see is not the one you wrote. It includes other people's work, so a failure can come from an interaction rather than from your own code.
You can see these branches while a queue is active:
git ls-remote origin 'refs/heads/gh-readonly-queue/*'The output is empty when the queue is idle. It is useful when you need to find the exact commit CI is testing.
Merge method and the final commit
When a group passes, GitHub updates the base branch using the merge method configured for the queue: merge commit, squash, or rebase. The method applies to each pull request as it merges, and the temporary commit is built with the same method so that what you tested matches what you ship. Choosing the method is part of queue setup, which a later chapter covers.
When something goes wrong
The queue handles failure by changing the test candidates of the entries behind the failure:
- If a pull request's checks fail, GitHub removes it from the queue.
- Every entry behind it is rebuilt without it, and its checks run again on the new candidate. Their earlier results are no longer valid, because the code they tested will not exist.
- Entries ahead of the failing pull request are unaffected.
Removal is not permanent. Once the author fixes the problem, the pull request can be added again at the back of the queue.
This design costs extra CI runs when failures happen. A failure near the front of a long queue restarts every entry behind it. Teams with flaky tests feel this most, and it is the main reason queue settings exist. Later chapters on grouping strategy and cost tuning address them.
How the queue differs from "require branches to be up to date"
Branch protection has a setting that requires a pull request branch to be current with the base branch before it can merge. That setting gives a similar guarantee, but it works by forcing each author to update and re-test manually after every other merge.
| Behavior | Require up-to-date branches | Merge queue |
|---|---|---|
| Who updates the branch | The author, by hand | GitHub, automatically |
| Testing after another merge | Restarts from scratch, one pull request at a time | Already includes the entries ahead |
| Concurrency | Effectively serial for authors | Tests several candidates at once |
| Scales to busy branches | Poorly, because of rebase races | Designed for it |
On a branch with a few merges a day, the manual approach is fine. On a branch with dozens of merges a day, authors keep invalidating each other's results, and the queue removes that churn.
What the queue does not change
Your authoring workflow stays mostly the same. You still open a pull request, get review, and see checks run on the branch. The queue adds one step at the end: instead of merging, you add the pull request to the queue. Required reviews and required checks on the pull request still apply before it can be queued. The queue then adds a second round of checks on the merge group.
That second round is why every required check needs to run in both places. A check that only runs on pull requests will never report on a merge group, and the queue will wait on it until it times out. The next chapters show how to avoid that.
Exercise
Pull requests A, B, and C are in the queue in that order. Each has a temporary branch under gh-readonly-queue/. What code does the temporary branch for C contain, and what happens to C's checks if B fails?
Show answer
C's branch contains the latest base branch plus A, B, and C. If B fails, GitHub removes B from the queue and rebuilds C's merge group without B, so C contains the base branch, A, and C. C's checks run again on that new candidate. A is unaffected because it is ahead of the failure.
Exercise
A team's workflow runs required tests only on the pull_request event. They enable a merge queue, and the first queued pull request never merges. Why, and what does this tell you about how the queue decides a group is ready?
Show answer
The queue tests a temporary merge group branch, and the workflow never runs for that event, so the required check never reports a status on the merge group commit. The queue only treats a group as ready when the required checks report success on that group commit, so it waits until it times out. The workflow needs to run on the merge_group event as well.