Sub topic 10
Enforcing linear history with branch protection
This chapter assumes you can read git log --graph output. It also assumes you know the three merge methods from the earlier chapters: merge commit, squash and merge, and rebase and merge. Here you will see how GitHub can stop merge commits from entering a branch. You will also see what that rule does and does not guarantee, and which errors people hit when they turn it on.
What "linear history" means
A history is linear when every commit has exactly one parent. Reading it from the tip back to the root gives a single line, with no branches rejoining. A merge commit is the only kind of commit with two parents, so a linear history contains no merge commits.
The Require linear history rule is a branch protection setting. It rejects any update to a protected branch that would add a merge commit. It is a rule about the shape of the graph, not about review or tests.
What the rule blocks and what it allows
The rule applies to every way a commit can reach the protected branch.
| Way of updating the branch | Result with linear history required |
|---|---|
| Pull request merged with "Create a merge commit" | Not available |
| Pull request merged with "Squash and merge" | Allowed |
| Pull request merged with "Rebase and merge" | Allowed |
| `git push` of a local merge commit | Rejected |
| `git push` of ordinary single-parent commits | Allowed, unless another protection rule forbids it |
Two details matter.
- The rule looks only at new commits. Merge commits already in the branch stay where they are. Turning the rule on does not rewrite anything.
- The rule does not require pull requests. Someone can still push plain commits directly to the branch unless you also enable Require a pull request before merging.
How it fits with the repository merge settings
Two separate settings control merging, and they interact.
- Repository settings, Pull Requests section: this turns the three methods on or off for the whole repository.
- Branch protection or ruleset: this adds the linear history requirement for specific branches.
The linear history rule does not change the repository settings. If "Allow merge commits" stays on, the merge commit option may still be listed on a pull request, but GitHub will not let you use it against a protected branch. For a cleaner interface, turn off "Allow merge commits" in the repository settings as well. Contributors then never see an option that cannot work.
The reverse mistake is more serious. If you require linear history and also turn off both squash and rebase merging, no merge method is left and nobody can merge through the web UI. Keep at least one of squash or rebase enabled.
Turning the rule on in the web UI
There are two places to configure it. Both need admin access to the repository.
Classic branch protection
- Open the repository, then Settings, then Branches.
- Under Branch protection rules, choose Add rule (or edit the existing rule).
- In Branch name pattern, enter the branch, for example
main. - Tick Require linear history.
- Save the rule.
Rulesets
Rulesets are the newer mechanism and can apply to several branches or to a whole organization.
- Open Settings, then Rules, then Rulesets.
- Create a new branch ruleset and set its target branches, for example the default branch.
- Enable the Require linear history rule.
- If you want to restrict methods per branch, also enable Require a pull request before merging and choose its allowed merge methods.
- Set the enforcement status to Active.
A ruleset can also limit the allowed merge methods for pull requests on its own. That gives you linear history and a method list in one place, so the two settings cannot drift apart.
Turning the rule on from the command line
The REST API sets the same option with the field required_linear_history. The classic protection endpoint replaces the whole rule on each call, so you must send every top-level field. Use null for the ones you do not want.
gh api --method PUT repos/OWNER/REPO/branches/main/protection --input - <<'EOF'
{
"required_status_checks": null,
"enforce_admins": true,
"required_pull_request_reviews": {
"required_approving_review_count": 1
},
"restrictions": null,
"required_linear_history": true
}
EOFReplace OWNER and REPO with real values. Because this call overwrites the existing rule, read the current settings first with gh api repos/OWNER/REPO/branches/main/protection. Otherwise you may remove checks that were already configured.
Verifying the result
Do not rely on the checkbox alone. Ask Git whether the branch actually has merge commits.
git fetch origin
git log --merges --oneline origin/main
git rev-list --merges --count origin/mainThe first command lists merge commits and the second counts them. On a branch that has been linear since the beginning, the list is empty and the count is 0. On an older repository you will see merge commits from before the rule. To check only the recent period, add a range such as origin/main --since="2026-01-01" to the git log command.
To see the shape of the line, use the graph view from earlier in the tutorial:
git log --oneline --graph --decorate origin/mainA linear branch shows a single column of * markers with no branching or joining lines.
Common errors and gotchas
A local merge is rejected on push
A contributor runs git merge main on a local copy of main and pushes. GitHub refuses the push with a GH006 "Protected branch update failed" error. The fix is to avoid creating the merge commit. Use git pull --rebase, or reset the local branch to the remote and replay the work through a pull request.
git fetch origin
git switch main
git reset --hard origin/mainRun reset --hard only after saving any local work on another branch, because it discards uncommitted changes and moves the branch pointer.
The pull request has no usable merge button
The cause is usually one of three things.
- The repository allows only merge commits, so nothing is left once linear history is required.
- The pull request branch has conflicts. Squash merges can fail on conflicts just like other methods.
- A rebase merge cannot be replayed cleanly on the web. As the earlier chapter on rebase limits explained, resolve it locally.
Updating the branch creates a merge commit
The Update branch button on a pull request defaults to merging the base branch into the feature branch. That makes a merge commit on the feature branch, not on main. The rule does not block it, and a squash merge will hide it. If you plan to use rebase and merge, a feature branch full of "Merge branch main" commits is awkward, so use Update with rebase from the button's dropdown. Rewriting the branch this way changes its commit SHAs, so anyone else working on it must refetch.
Administrators can bypass the rule
By default, protection rules do not apply to repository administrators. To make them follow the rule, tick Do not allow bypassing the above settings in classic protection (the API field enforce_admins). In a ruleset, leave the bypass list empty.
Merge queues use the same restriction
If you use a merge queue, its merge method also has to be compatible with the rule. Pick squash or rebase for the queue when linear history is required.
When linear history is the right choice
The rule gives you a history that is simple to read with git log, easy to bisect, and free of merge-commit noise. It also changes what you can do afterward.
- Reverts are simpler. A reverted squash commit or rebased commit is a plain
git revert <sha>. You never need-m 1. - You lose the record of the branch. With squash, individual branch commits vanish from the base branch. With rebase, the commits stay but receive new SHAs.
- Follow-up branches need care. After a squash or rebase merge, a branch built on the old feature branch needs
git rebase --onto, as covered in the stacked branches chapter.
If your team values seeing where each branch was merged, the rule works against you. Use it when a readable single line matters more than a record of branch topology.
Exercise
A repository has "Allow merge commits" turned on, "Allow squash merging" and "Allow rebase merging" turned off, and the main branch requires linear history. A developer opens a pull request that passes all checks. What happens when they try to merge it in the web UI, and what is the smallest change that fixes it?
Show answer
They cannot merge it. The linear history rule blocks the only enabled method, the merge commit, and the other two methods are off. The smallest fix is to enable squash merging or rebase merging (or both) in the repository's Pull Requests settings. Turning off "Allow merge commits" as well is a good follow-up, so the interface does not offer an option that can never succeed.
Exercise
You enabled "Require linear history" on main last week. Write a command that confirms no merge commits were added since then, and explain why running it on all of main might still print results.
Show answer
git log --merges --oneline --since="1 week ago" origin/main lists merge commits from the last week, and an empty result confirms none were added. Running it on all of main can still print results because the rule only blocks new merge commits. Merge commits created before the rule was enabled stay in the history.