Sub topic 3
Merge groups and temporary branches
This chapter assumes you know Git branching and merging, pull requests, required status checks, and the basics of GitHub Actions workflow syntax. It covers what GitHub builds behind the scenes when a pull request enters a merge queue. You need this model before you configure workflows or settings in later chapters.
What a merge group is
A merge group is the unit of work that GitHub tests and merges. It has three parts:
- A base: the current tip of the target branch, or the tip of the previous merge group in the queue.
- One or more pull requests: the changes being tested together.
- A temporary branch: a branch GitHub creates to hold the combined result.
When a pull request enters the queue, GitHub does not test the pull request branch again as it is. It builds a new commit that contains the latest base plus your changes, and it runs your checks on that commit. If the checks pass, GitHub fast-forwards the target branch to that commit. The commit you tested is the commit that lands.
This is what closes the gap described in the earlier chapters. Passing checks on a pull request branch only prove that the branch works with the base it was last updated against. The merge group proves that the change works with the base it will actually be merged into.
The temporary branch
GitHub creates the temporary branch under a reserved prefix. Its name has this shape:
gh-readonly-queue/main/pr-1234-4f9c2ab7d1e03b5c8a6f7d2e91b0c3a4d5e6f708The parts are:
gh-readonly-queue/is the fixed prefix. The name tells you the branch is managed by GitHub.mainis the target branch the group will merge into.pr-1234is the number of the pull request that the group was created for. For a group with several pull requests, this is the last pull request in the group.- The trailing hash is the SHA of the base commit that the group was built on.
Treat these branches as read-only. You should not push to them, rebase them, or delete them by hand. GitHub deletes them after the group merges or fails. Because they are created and removed constantly on a busy repository, avoid tooling that reacts to every new branch, such as a workflow that triggers on any push.
How groups are built and ordered
The queue is first in, first out. Each merge group is built on top of the one ahead of it, so the queue forms a chain.
Suppose main is at commit M, and three pull requests enter the queue in the order A, B, C. With a group size of one, GitHub builds this:
main: M
group A: M + A
group B: M + A + B
group C: M + A + B + CEach group tests the speculative future of main. Group B assumes that A will merge. Group C assumes that A and B will both merge. Because the groups are built together, GitHub can run their checks in parallel. The setting that controls how many groups build at once is covered in the chapter on queue settings.
The diagram shows the same chain.
Merging still happens in order. Group B may finish its checks before group A does, but GitHub waits for A to merge first. A group that passes while an earlier group is still running is held until the earlier group is resolved.
Merge groups with several pull requests
The group size setting lets one merge group contain several pull requests. With a maximum of three, A, B, and C could be tested as one commit tree: M + A + B + C. This costs one CI run instead of three.
The trade-off is in failure handling. If a combined group fails, GitHub cannot tell from the single result which pull request caused the failure. The grouping strategy setting decides how that is handled. The chapter on grouping strategy covers it, so this chapter only notes that group size changes what a "group" means. A group can be one pull request or many.
What happens when something changes
The queue's speculative state has to stay true. GitHub rebuilds groups when the assumptions behind them change.
- If a pull request is removed from the queue, every group behind it was built on a base that included that pull request. GitHub discards those groups and builds new ones without it. The checks run again.
- If a group fails its checks, the pull request in it is removed. Groups behind it are rebuilt without it.
- If someone pushes directly to the target branch, for example an administrator who bypasses the queue, the base changes. Queued groups are rebuilt on the new base.
Rebuilding is why a failure near the front of a long queue is expensive. Every entry behind it repeats its checks.
Why the branch name matters to your workflows
The temporary branch is where your required checks must run. A workflow that only triggers on pull_request will never run on gh-readonly-queue/..., because no pull request event is fired for the temporary branch. The required check then never reports a status on the merge group commit, and the group waits until the timeout.
GitHub fires a separate event, merge_group, for these commits. The next chapters cover the trigger in detail. For now, this is the smallest workflow that reports a status in both places:
name: CI
on:
pull_request:
branches: [main]
merge_group:
types: [checks_requested]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
run: make testTwo details are worth noting:
- In a
merge_grouprun,github.refis the temporary branch, for examplerefs/heads/gh-readonly-queue/main/pr-1234-.... Do not write logic that comparesgithub.reftorefs/heads/mainand expects it to match. actions/checkoutchecks out the merge group commit by default for this event, so the tests run against the combined code, not against your pull request alone.
You can inspect the merge group in the event payload. The merge_group object holds the commit SHAs and refs that GitHub used. This job prints them, which helps when you debug a queue.
name: Inspect merge group
on:
merge_group:
jobs:
inspect:
runs-on: ubuntu-latest
steps:
- name: Print merge group details
run: |
echo "head ref: ${{ github.event.merge_group.head_ref }}"
echo "head sha: ${{ github.event.merge_group.head_sha }}"
echo "base ref: ${{ github.event.merge_group.base_ref }}"
echo "base sha: ${{ github.event.merge_group.base_sha }}"The head_ref value is the temporary branch name. The base_sha is the commit the group was built on. When it is the previous group's head commit, you are looking at a group that is not first in the chain.
Common mistakes
| Symptom | Likely cause |
|---|---|
| The queue shows "Waiting for status to be reported" | The required workflow has no `merge_group` trigger, so no check runs on the temporary branch. |
| A branch named `gh-readonly-queue/...` appears and vanishes | This is normal. GitHub creates and removes it for each merge group. |
| A pull request passes its checks but fails in the queue | The combined state differs from the pull request alone. This is the problem the queue exists to catch. |
| Several pull requests re-run checks after one is removed | Groups behind the removed entry are rebuilt without it. |
Exercise
A queue contains pull requests A, B, and C in that order, each in its own merge group. Pull request A fails its checks. Which commits does GitHub test next, and why?
Show answer
GitHub removes A from the queue and discards groups B and C, because both were built on a base that included A. It rebuilds B on the current tip of main (M + B) and C on top of that (M + B + C), and runs their checks again. B and C are not merged with A's changes, so their results reflect what will really land.
Exercise
Your required check is called "test", and its workflow has only an on: pull_request trigger. A pull request enters the queue and stays in "Waiting for status to be reported" until it times out. What is wrong, and what is the fix?
Show answer
The merge group commit lives on a temporary gh-readonly-queue branch, and pullrequest events do not fire for it, so the "test" check never reports. Add `mergegroup with types: [checks_requested] (or no types) to the workflow's on:` block, and do not restrict it to the main branch.