Sub topic 1
Why a Busy Main Branch Breaks with Green Pull Requests
Prerequisites
This chapter assumes basic Git branching and merging, pull requests, GitHub Actions, and branch protection rules or repository rulesets.
Why Green Pull Requests Can Still Break Main
A pull request can pass every check and still fail after it merges. Its checks usually test the pull request branch against the base commit that existed when the checks ran. Meanwhile, another pull request may merge first.
The two changes can conflict semantically even when Git merges them cleanly. For example, one pull request changes a function’s output and another changes its caller. Each branch passes tests alone, but the combined code fails. A busy main branch can therefore become red immediately after merging green pull requests.
A merge queue reduces this race by testing changes in the order they will enter the base branch.
What a Merge Queue Does
A merge queue is an ordered list of approved pull requests waiting to merge. The queue creates a proposed merge for the next pull request, or for a compatible group of pull requests, using the latest main. It runs the required checks against that proposed result.
Only a merge group with passing required checks is merged. If main changes while a pull request waits, the queue rebuilds its proposal. This makes the tested commit much closer to the commit that will actually become main.
Merge Groups and Temporary Branches
The queue represents each proposed combination with a temporary, read-only branch. GitHub commonly uses names beginning with gh-readonly-queue/. This branch contains the current base branch plus the queued pull request changes.
The temporary branch is not a normal development branch. Do not commit to it or use it as a permanent integration branch. Its purpose is to give CI a precise commit to test.
Turning on the Merge Queue
A repository administrator enables the queue for the protected main branch through a branch protection rule or a repository ruleset. The configuration normally specifies:
- The merge method, such as squash or merge commit.
- How many pull requests may build together.
- How many merge groups may run concurrently.
- How long required checks may take before timing out.
- The grouping strategy, such as requiring every pull request in a group to pass.
After enabling it, workflows must also run for the merge_group event. Required checks that run only on pull_request will never report on the temporary queued commit, so the queue can remain stuck.
on:
pull_request:
merge_group:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make testExercise
Why can two individually green pull requests produce a failing main branch?
Show answer
Their checks may test each branch separately, while their combined changes interact differently after both merge.