I was cleaning up the HygroMonitor repository this week and noticed my habit: I push straight to main and look at the CI result afterwards. When it is red, main is already broken. So I set up branch protection on my own repositories. I used to think it was only for teams.
Why a PR flow helps when you are alone
Two reasons.
CI runs before the change is on main, not after. With a required status check, GitHub does not let you merge until the checks pass. My GitHub Actions CI setup already runs on pull_request, so nothing new is needed there.
A PR is a record of why. The commit message says what changed. The PR description is where I write why, what I tried, and a link to the issue. Six months later, git blame gives me a commit, and GitHub shows me the PR that brought it in. I read my own old PRs more often than I expected.
The cost is small: one branch, one gh pr create, one merge button.
git switch -c fix-retry-timeout
# ... commit ...
git push -u origin fix-retry-timeout
gh pr create --fill
# after CI is green
gh pr merge --squash --delete-branch
What to turn on
In Settings > Branches, add a rule for main:
- Require a pull request before merging. But turn off “Require approvals” (see below).
- Require status checks to pass before merging. Pick the jobs from your workflow. A check only appears in the search box after it has run at least once on the repository, so push a branch first. The name is the job name, for a matrix it is one entry per combination, like
test (3.11). - Require branches to be up to date before merging. Optional. For a solo project the branch is almost always up to date anyway.
- Do not allow bypassing the above settings. Without it, as the admin you can still push to
maindirectly, and the rule only protects you from other people. I turned it on. It is annoying sometimes, and that is the point.
Force pushes and deletions of main are blocked by default once a rule exists.
Optional: linear history and signed commits
Require linear history means only squash or rebase merges, no merge commits. In my rebase vs merge notes I preferred --no-ff merge commits for features in a team. For a solo repo I now just squash every PR: one PR is one commit on main, and the PR link is in the commit message. So I turned linear history on. If you like merge commits, leave it off. It is a matter of taste here.
Require signed commits works with SSH signing too. GitHub has shown SSH signatures as verified since August 2022, and you add the key under Settings > SSH and GPG keys as a signing key, not only as an authentication key. Be careful with squash and rebase merges done on the website: GitHub signs those commits with its own key, so they pass. But if you rebase locally on a machine without your key, the push is rejected. I left this off on the repos I also edit from the Pi.
What not to turn on when you work alone
Require approvals. You cannot approve your own pull request on GitHub. With one required approval and nobody else in the repo, every PR is blocked forever, unless you bypass the rule, which makes it useless. Same for “Require review from Code Owners”.
Restrict who can push, require deployments to succeed, merge queue: these solve team problems. I skip them.
Classic rules or the new rulesets
GitHub released repository rulesets in public beta in April 2023, and made them generally available on 24 July 2023. They are under Settings > Rules > Rulesets, next to the classic branch protection. The rules are mostly the same ones listed above. The differences that matter to me:
- Several rulesets can apply to the same branch, and they are combined. With classic rules, only one rule matches a branch.
- A ruleset has an explicit bypass list and an on/off status. You can disable it without deleting it.
- Rulesets can target tags too, so you can stop yourself from deleting or moving a release tag.
- People with read access can see which rules apply to a branch, without admin rights.
Both work on public repositories with a free account. For private repositories on a free personal account, neither is enforced; you need a paid plan.
For a new repo I use a ruleset now: one for main (PR, status checks, linear history, no bypass), and one for tags v* that blocks deletion and updates. On old repos I left the classic rules alone. They still work, and I did not see a reason to move them yet. You can have both on one repo, and then both apply, which is confusing. Pick one per repository.