Learning center

Should you require linear history on the default branch?

Require linear history if you already squash or rebase every pull request. It then costs little and keeps git revert and git bisect simple. It only forbids merge commits on the branch. It does not prove that each commit on the branch was tested, or that it matches what reviewers approved.

What the rule enforces#

GitHub's description is short: "Enforcing a linear commit history prevents collaborators from pushing merge commits to the targeted branches or tags. This means that any pull requests merged into the branch or tag must use a squash merge or a rebase merge." The repository must allow at least one of those methods first. The only benefit the documentation claims is that "A strictly linear commit history can help teams revert changes more easily." Require linear history

The rule exists in rulesets and in classic branch protection with nearly the same wording. About protected branches It also holds against force pushes: "if a branch requires a linear commit history, you cannot force push merge commits to that branch."

What a merge commit does to revert and bisect#

These runs use a throwaway local repository, so they show Git's behavior. Progress and date lines are trimmed from the output. A feature branch with two commits was merged into main with a merge commit while main also moved:

$ git log --oneline --graph
*   7d0c556 Merge pull request from feature
|\
| * 453ce32 Feature part 2
| * dfff5fe Feature part 1
* | 0a5c18e Unrelated fix on main
|/
* 9b87ae6 Base
$ git revert --no-edit HEAD
error: commit 7d0c556e12cc672ec27a650fee9ab4fe0587606f is a merge but no -m option was given.
fatal: revert failed
$ git revert --no-edit -m 1 HEAD
[main 5800ad0] Revert "Merge pull request from feature"

Reverting the merge works once you name the parent to keep, but the person reverting during an incident has to know that -m 1 means "keep main's side." A squash merge would have left one ordinary commit, reverted with git revert and nothing else.

Bisect has a related problem. In a second repository with the same shape, a three-commit feature branch merged into a main that had two commits of its own, a plain git bisect between the base commit and main started on a feature-branch commit:

$ git bisect start main 351aaf8
[8f243b1ace73b12315dc60541171ab2b525d8d92] feature WIP 3
$ git bisect start --first-parent main 351aaf8
[ca3650a2f3cf333a8d166da69f07512c3bc397e1] main 2

feature WIP 3 was never the state of main. It lacks the two main commits, so testing it answers a question about a branch nobody deployed. --first-parent keeps bisect on main's own history, which a linear branch gives you without the flag.

What it does not prove#

Linear history describes the shape of the branch, not its content. Two points matter for review policy.

First, a rebase merge rewrites the commits it lands. GitHub's Rebase and merge "Always updates the committer information and creates new commit SHAs," and it "Drops commits that were empty to begin with." About merge methods on GitHub The intermediate commits land on a base they were never tested on. In the same local repository, rebasing the two feature commits onto the new main gave the first one a different tree:

$ git log --oneline main..feature
453ce32 Feature part 2
dfff5fe Feature part 1
$ git rebase main
Successfully rebased and updated refs/heads/feature.
$ git log --oneline main..feature
59c82e1 Feature part 2
6aeed6c Feature part 1
$ git ls-tree --name-only dfff5fe
a.txt
f.txt
$ git ls-tree --name-only HEAD~1
a.txt
b.txt
f.txt

The pull request's commits were dfff5fe and 453ce32. The branch now holds 6aeed6c and 59c82e1, and the first of them is a tree that existed nowhere before the rebase, so no check ran on it. A linear main with rebase merges can therefore contain commits that never ran a check. A squash merge lands one commit, the pull request combined with the current base. If main moved after the pull request's last check run, no check ran on that combination either, unless a merge queue or Require branches to be up to date before merging forces one. The up-to-date branch guide compares those options.

Second, linear history does not show what was approved. A squashed commit says nothing about the review that preceded it, and rebased commits carry new SHAs. The record of review lives in the pull request.

The cost#

If your repository already allows only squash merging, the rule changes almost nothing for pull requests and adds one stop: nobody can push a merge commit made locally, for example a release branch merged back into main by hand.

If your team uses merge commits, the cost is real. GitHub's documentation lists what squashing loses: "You lose information about when specific changes were originally made and who authored the squashed commits," and long-running branches that keep going after a squash merge bring back already-merged commits and repeated conflicts. Rebase merging can require contributors to "rebase on the command line, resolve any conflicts, and force push their changes." About merge methods on GitHub

Two interactions are worth checking before you enable it. With a merge queue, "you no longer get to choose the merge method, as this is controlled by the queue," so the queue's method must be squash or rebase. With Require signed commits, rebase merges come with a catch: GitHub adds the rebased commits "without commit signature verification." The signed commits guide covers that case.

The decision#

Turn on linear history for the default branch if you squash-merge, or are ready to. Prefer squash over rebase merges when CI runs per pull request, so the branch holds only commits that correspond to a reviewed pull request. Leave it off on branches where merge commits carry meaning, such as a release branch that receives back-merges, and use git bisect --first-parent there instead.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading