# Set Up a GitHub Merge Queue

_A merge queue tests each pull request against the target branch plus every pull request ahead of it, then merges the ones that pass in order._

## Goal

Keep `main` green when many pull requests land each day, without asking every author to update their branch and rerun CI before merging. This guide covers enabling GitHub's merge queue, wiring CI to it, using it day to day, and landing [stacked pull requests](/learn/concepts/git/stacked-prs) through it.

## What a merge queue does

Two pull requests can each pass CI against an old `main` and still break `main` together. GitHub's **Require branches to be up to date before merging** rule prevents that by making each author update and rerun CI before merging, one at a time. A **merge queue** does that work for you.

When you add a pull request to the queue, GitHub builds a temporary branch from the latest base branch, the pull requests ahead of it, and your pull request. CI runs on that combination. Say pull requests #1 and #2 are queued for `main`:

| Temporary branch | Contains |
|---|---|
| `main/pr-1` | `main` + #1 |
| `main/pr-2` | `main` + #1 + #2 |

When both pass, GitHub merges `main/pr-2` and `main` gains both changes. If #1 fails, GitHub removes it from the queue, rebuilds `main/pr-2` with only #2, and merges #2 when it passes. The pull request timeline records why a pull request left the queue.

## Check availability

Per GitHub's docs, merge queues are available in public repositories owned by an organization, and in private repositories owned by organizations on GitHub Enterprise Cloud. A repository owned by a personal account does not get one. You need admin permissions on the repository to turn it on.

## Trigger CI on merge groups

Do this before you require the queue. The queue waits for your required checks to report on the temporary branch, and a check that never runs never reports. With GitHub Actions, add the `merge_group` event to every workflow that produces a required check:

```yaml
on:
  pull_request:
  merge_group:
```

For a third-party CI provider, run builds on pushes to branches that start with `gh-readonly-queue/{base_branch}`. Those are the temporary branches, and their commit SHA differs from the pull request's.

## Enable the queue

1. Open the repository's **Settings**, then the branch protection rule or ruleset for `main`.
2. Turn on **Require merge queue**.
3. Set the options below, then save.

| Setting | What it controls |
|---|---|
| Merge method | Merge, rebase or squash for queued pull requests |
| Build concurrency | How many `merge_group` builds run at once, 1 to 100 |
| Only merge non-failing pull requests | When off, a failed pull request can still merge if the last pull request in its group passes |
| Status check timeout | How long the queue waits for CI before it treats the checks as failed |
| Merge limits | Minimum and maximum pull requests per merge, and how long to wait for the minimum |

A branch protection rule whose name pattern uses a `*` wildcard cannot require a merge queue. Pick **Squash** as the merge method if you want one commit per pull request on `main`. [Git Squash Commits](./squash-git-commits.md) shows what that commit contains.

## Add a pull request to the queue

Once the pull request is approved, click **Merge when ready**, then **Confirm merge when ready**. If required checks have not passed yet, GitHub turns on auto-merge and queues the pull request when they do. Click **Disable auto-merge** to cancel that.

From the terminal, `gh pr merge` does the same:

```bash
gh pr merge 123
```

On a branch that requires a merge queue, the command queues the pull request when checks have passed and enables auto-merge when they have not. The CLI cannot remove a pull request from the queue or show the queue. Use **Remove from queue** on the pull request, or the queue page linked from the **Branches** page.

A pull request can also jump to the top of the queue when you add it. Every in-progress build then restarts, so save it for urgent fixes.

## Land stacked pull requests through the queue

A stack is a chain of pull requests where each targets the branch below it. With GitHub's native stacked pull requests, the queue takes the whole stack. GitHub adds the stack's pull requests to the queue in order, bottom first. If one fails or is removed, every pull request above it leaves the queue too, because each one depends on the code below it.

GitHub keeps a stack in one merge group even when that exceeds the maximum group size by up to 50 percent. A larger stack moves to the next merge group as a single unit. After a merge, GitHub rebases the next unmerged pull request onto the stack's base.

If you manage stacks with plain branches instead, only pull requests that target `main` enter the queue. Merge the bottom one, retarget the next to `main`, and restack it. With a squash merge, use `git rebase --onto` as shown in [Rebase a Stacked Branch After a Squash Merge](/learn/answers/rebasing-and-stacks/rebase-stack-after-squash-merge).

## How Treq handles it

Treq does not queue or merge a pull request on GitHub for you. It shows each pull request's checks in the workspace header and the GitHub panel, as covered in [Creating and Viewing Pull Requests](/docs/how-to/creating-and-viewing-pull-requests). When a lower workspace's pull request merges, Treq moves the workspace above it onto that workspace's target, so the next layer of a [stacked workspace](/docs/concepts/workspaces#stacks-and-rebasing) is ready to queue.

## Sources

- [Managing a merge queue](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/configuring-pull-request-merges/managing-a-merge-queue)
- [Merging a pull request with a merge queue](https://docs.github.com/en/pull-requests/how-tos/merge-and-close-pull-requests/merging-a-pull-request-with-a-merge-queue)
- [Merging stacked pull requests](https://docs.github.com/en/pull-requests/how-tos/merge-and-close-pull-requests/merging-stacked-pull-requests)

## Related

- [What are Stacked PRs?](/learn/concepts/git/stacked-prs): split a large change into dependent pull requests
- [Stacked PR Workflow](/learn/workflows/git/stacked-pr): build, review and land a stack
- [Merge vs Rebase](/learn/concepts/git/merge-vs-rebase): choose the merge method the queue uses
