Sub topic 10
Enforcing linear history with branch protection
This chapter assumes you can read git log --graph output and know the three GitHub merge methods: merge commit, squash, and rebase. It covers the "Require linear history" rule, what it blocks, and the errors teams hit when they turn it on.
What a linear history is
A linear history is a base branch where every commit has exactly one parent. There are no merge commits, so git log --graph shows a single straight line.
Squash and rebase merges already produce that shape. A merge commit has two parents, so it breaks it. The rule exists to stop merge commits from reaching the protected branch.
Turning the rule on
The rule is part of branch protection (a branch protection rule or a ruleset). In the web UI:
- Open the repository, then Settings, then Branches.
- Add a branch protection rule for
main, or edit the existing one. - Select Require linear history.
- Save the rule.
If you use rulesets, the equivalent option is Require linear history in the ruleset's rules list.
What the rule blocks
The rule rejects any push to the protected branch that would add a merge commit. That includes the Create a merge commit button on a pull request and a direct git push of a locally merged branch.
git switch main
git merge feature # creates a merge commit if main has moved on
git push origin main # rejected when linear history is requiredSquash and rebase merges still work, because each one adds only single-parent commits.
Interaction with repository merge settings
The linear history rule and the allowed merge methods are separate settings, and they should agree.
- If the repository allows merge commits but the branch requires linear history, the Create a merge commit option does not work for that branch. GitHub disables it for pull requests into that branch.
- Allowing only squash or rebase in repository settings gives the same result with a clearer interface. Contributors never see an option that will fail.
| Setting | Where it lives | What it controls |
|---|---|---|
| Allowed merge methods | Repository settings | Which buttons appear on pull requests |
| Require linear history | Branch protection or ruleset | Which commits the branch accepts |
Set both. The first guides people, and the second enforces the result for every path, including pushes from the command line.
A diagram of the decision
Common errors
- The merge button is greyed out or missing. The pull request only has merge commit available. Enable squash or rebase in repository settings.
- A push is rejected with a "merge commits" message. You merged locally. Run
git rebase mainon your feature branch, then merge by squash or rebase through the pull request. - Merge commits already exist in history. The rule is not retroactive. It checks new pushes only, and old merge commits remain.
- Admins bypass the rule. If the protection rule does not apply to administrators, they can still push merge commits. Enable Do not allow bypassing the above settings if you want no exceptions.
- Updating a branch from
mainfails. The Update branch button on a pull request mergesmaininto the feature branch, which is not the protected branch, so it still works. Rebase instead if you want the feature branch to stay linear too.
Trade-offs
Linear history makes git log, git bisect, and reverts simpler, because each commit has one parent and git revert needs no -m flag. The cost is that you lose the record of where a branch started and ended, and rebase merges rewrite commit hashes, so the SHAs on main differ from those on the pull request branch.
Exercise
Your repository allows all three merge methods and main requires linear history. A teammate opens a pull request and finds that Create a merge commit does not work. What are two ways to fix their workflow, and which setting would you change so the problem does not confuse others?
Show answer
They can use Squash and merge or Rebase and merge, both of which add only single-parent commits. To avoid confusion, disable "Allow merge commits" in the repository's merge settings so the unusable option no longer appears.