Sub topic 6
Protecting history: force pushes, deletions, linear history, signed commits
This chapter assumes you know how a classic branch protection rule is created and how a ruleset targets branches. It covers the four settings that protect the commit history of a branch.
What each setting prevents
| Setting | Prevents | Classic rule | Ruleset rule |
|---|---|---|---|
| Force pushes | Rewriting history with `git push --force` | `allow_force_pushes: false` | `non_fast_forward` |
| Deletions | Removing the branch | `allow_deletions: false` | `deletion` |
| Linear history | Merge commits on the branch | `required_linear_history` | `required_linear_history` |
| Signed commits | Commits without a verified signature | `required_signatures` | `required_signatures` |
A force push replaces commits that others may already have pulled, so it can erase work. A deletion removes the branch and its protection with it. In a classic rule, both are blocked as soon as the rule exists, unless you explicitly allow them. Rulesets work the other way around. A rule type blocks something only if you add it to the ruleset.
Linear history and signed commits
Requiring linear history rejects merge commits. Pull requests can still land through squash merge or rebase merge. Enable it only if your team has disabled the "Create a merge commit" option on the repository, or contributors will see a blocked merge button.
Requiring signed commits means every commit pushed to the branch must have a verified signature. GitHub verifies GPG, SSH, and S/MIME signatures. Contributors must register their signing key on their account first. Commits made in the web interface are signed by GitHub, but check that the merge method you allow produces verified commits.
Enabling all four in a ruleset
This body goes to the rulesets endpoint. Setting enforcement to evaluate records violations without blocking them, so you can test first.
{
"name": "protect-history",
"target": "branch",
"enforcement": "evaluate",
"bypass_actors": [],
"conditions": {
"ref_name": { "include": ["~DEFAULT_BRANCH"], "exclude": [] }
},
"rules": [
{ "type": "deletion" },
{ "type": "non_fast_forward" },
{ "type": "required_linear_history" },
{ "type": "required_signatures" }
]
}gh api --method POST repos/OWNER/REPO/rulesets --input history.jsonFor a classic rule, set allow_force_pushes and allow_deletions to false and required_linear_history to true in the protection body. Signed commits use a separate endpoint, POST /repos/{owner}/{repo}/branches/{branch}/protection/required_signatures.
Common errors
- Signed commits are required, but a contributor's earlier commits in the pull request are unsigned. Merging fails until they re-sign and force push their feature branch. The feature branch is not protected, so this is allowed.
- Linear history is required, but the merge button offers only a merge commit. Enable squash or rebase merging in the repository settings.
- A bot pushes unsigned commits. Give the bot a signing key, or add it to the bypass list of a ruleset. Use bypass sparingly.
Exercise
A repository has a ruleset with only the deletion and required_linear_history rules on main. A developer runs git push --force origin main after rewriting the last commit. Is the push blocked by these rules? Why?
Show answer
No. Neither rule blocks a force push. Blocking force pushes needs the non_fast_forward rule. deletion only stops the branch from being deleted, and required_linear_history only rejects merge commits. Other rules, such as a pull request requirement, could still block the push.