Sub topic 1
Why a busy main branch breaks with green pull requests
Prerequisites
This chapter assumes you already know the following:
- Basic Git branching and merging, including what a merge base is.
- How pull requests and code review work on GitHub.
- What branch protection rules or repository rulesets do.
- How GitHub Actions workflows run and how a job becomes a required status check.
You do not need to know anything about merge queues yet. This chapter explains the problem that merge queues solve. Later chapters cover the queue itself.
The assumption behind "green means safe"
Most teams protect main with a rule like this: a pull request needs an approving review and passing status checks before it can merge. The reasoning is simple. If the tests pass on the pull request, the code is safe to merge.
That reasoning has a hidden assumption. It assumes the tests ran against the code that main will contain after the merge. On a quiet repository this is usually true. On a busy one it often is not.
When you open a pull request, GitHub runs your checks against one of two things:
- The head of your branch, for a workflow triggered by
push. - A test merge commit that combines your branch with the current tip of the base branch, for a workflow triggered by
pull_request.
In both cases the result describes a snapshot taken at one moment. If main moves afterwards, the result describes a combination that no longer exists.
How checks go stale
Suppose main is at commit A. Two developers each open a pull request from A.
- Priya opens PR 101, which changes how a function is called.
- Marcus opens PR 102, which adds a new caller of that same function.
- CI runs on both. Each pull request tests green against
A. - Both are approved.
- Priya merges PR 101.
mainis now atB. - Marcus merges PR 102.
mainis now atC.
The checks on PR 102 ran against A, not against B. Nobody ever ran the tests on B plus Marcus's change. That combination is exactly what C contains.
Semantic conflicts
Git detects textual conflicts. If two pull requests edit the same lines, the second one cannot merge cleanly, and GitHub shows a conflict.
The problem above is different. The two changes touch different lines, often in different files, so Git merges them without complaint. The code is still broken, because the changes are incompatible in meaning. This is called a semantic conflict (also called a logical conflict).
Common examples:
- One pull request renames or removes a function. Another adds a new call to the old name.
- One pull request changes a function to require an extra argument. Another adds a caller that passes the old arguments.
- One pull request tightens a database constraint. Another adds a migration or a test fixture that violates it.
- One pull request upgrades a dependency. Another uses an API the new version removed.
- Two pull requests each add a test that passes alone, but the tests share global state and fail together.
Here is a small example in Python. Priya's change removes a default argument:
# main at commit A
def send_email(to, subject="No subject"):
print(f"To: {to}, Subject: {subject}")
send_email("[email protected]")# PR 101 (Priya): subject is now required
def send_email(to, subject):
print(f"To: {to}, Subject: {subject}")
send_email("[email protected]", "Hello")Marcus's pull request leaves the function alone and adds another caller in a different file:
# PR 102 (Marcus): new caller, written against commit A
from mailer import send_email
send_email("[email protected]")Each pull request is correct against A. After both merge, the second call raises TypeError: send_email() missing 1 required positional argument: 'subject'. Git saw no conflict, and no check on either pull request could have caught it.
Why GitHub's "up to date" setting is only a partial fix
Branch protection offers a setting called "Require branches to be up to date before merging". With it on, GitHub blocks the merge button until the pull request branch contains the latest main.
This closes the gap. After Priya merges, Marcus's pull request shows as out of date. He must update his branch, wait for CI to run again on the new combination, and only then merge.
The cost shows up as the team grows. Consider ten ready pull requests and a CI run that takes 15 minutes.
- All ten are green and approved.
- One merges. The other nine become out of date.
- Each developer must update and re-run CI. If two of them merge at nearly the same time, only one can succeed. The other has to update again.
- This repeats until the list is empty.
In the worst case, the number of CI runs grows with the square of the number of waiting pull requests. The wall-clock time is closer to ten sequential 15-minute runs, which is 150 minutes, and that assumes everyone is watching and clicking at the right moment.
| Approach | Safe against stale checks | Cost on a busy branch |
|---|---|---|
| Merge when the pull request is green | No | Low, but `main` can break |
| Require branches to be up to date | Yes | High. Developers race and re-run CI by hand |
| Merge queue | Yes | Lower. Testing and ordering are automated |
The race is also a human problem. Developers lose time rebasing and re-pressing buttons, and some will give up and turn the setting off. Then the stale-check risk is back.
What a broken main costs
A red main is more than an embarrassment. It has concrete effects:
- Every other pull request now fails, or inherits the failure, because they all build on
main. - Developers cannot tell whether their change caused a failure. They spend time investigating problems they did not create.
- Release and deployment pipelines that run from
mainstall or ship a bad build. - Someone has to find the culprit, then revert or fix forward. Meanwhile, merges are often frozen.
Each of these costs more as the team and the merge rate grow. A repository with two merges a day can survive the occasional red commit. A repository with 80 merges a day cannot.
Seeing the problem in a workflow
A typical workflow for the setup described above looks like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
run: make testOn pull_request, actions/checkout checks out a merge of the pull request and main as it was when the run started, or when the pull request last changed. If main moves later, the job does not re-run on its own. The green check on the pull request keeps its status even though the code it tested is no longer what would merge.
The idea that fixes it
The root cause is that the test and the merge happen at different times, against different bases. The fix is to make them happen against the same base.
Put differently, a pull request should be tested against the exact commit it will merge onto, which is the latest main plus every pull request that is going to land before it. If the test passes there, and the merge lands in that order, then the resulting main is the tested one.
Doing that by hand means serializing merges and re-running CI for each pull request in turn, which is the expensive "up to date" workflow above. A merge queue automates it:
- Developers add approved pull requests to a queue instead of merging directly.
- The queue builds a temporary branch for each entry, containing
mainplus all entries ahead of it plus that entry's changes. - CI runs on that temporary branch.
- When checks pass, the queue merges the entry into
mainin order. If they fail, the queue removes the entry and rebuilds the ones behind it.
Because the queue tests entries in parallel, each stacked on the one before it, throughput stays high even though the order is strict. The following chapters explain how this works and how to configure it.
Edge cases to keep in mind
- Rare problems still matter. A semantic conflict needs two pull requests that overlap in meaning within a short window. On a small team that is rare. The cost is not the frequency but the effect: one occurrence breaks
mainfor everyone. - Fast merges do not remove the problem. Even if CI takes two minutes, a window exists. Two pull requests merged within that window can collide.
- Flaky tests blur the picture. A failing
mainmay come from a flaky test rather than a semantic conflict. Check whether the failure reproduces before blaming the merge order. - Squash merges hide the history. With squash merging, the broken commit looks like a single change, so the interaction between two pull requests is not visible in the log. You have to reason from merge times.
- Rebasing does not help by itself. Rebasing your branch onto
mainmakes it current at that moment, but another pull request can still land right after.
Exercise
Two pull requests, A and B, both branch from the same commit on main. They edit different files, so Git reports no conflict. Each passes CI. A is merged, then B is merged, and main now fails its tests. Name this kind of problem and explain why no check on either pull request caught it.
Show answer
This is a semantic (logical) conflict. Each pull request was tested only against the old main, which contained neither the other's changes. The combination of A and B was never tested before it reached main, and Git only detects textual overlap, so it did not flag the incompatibility.
Exercise
A team turns on "Require branches to be up to date before merging" to prevent this problem. Ten approved pull requests are waiting and CI takes 15 minutes. Why does this setting become a bottleneck, and what does a merge queue change?
Show answer
After each merge, every other waiting pull request is out of date. Its author must update the branch and wait for another 15-minute CI run, and only one pull request can win each race. Merges are effectively serial, with much manual work and repeated CI runs. A merge queue builds temporary branches that stack each entry on main plus the entries ahead of it, runs CI on them automatically and in parallel, and merges them in order, so no one has to update branches by hand.