Protect main With a Ruleset

The new week is live again, and the missing comma is fixed. One part of Wednesday’s update still deserves a closer look, though. Your repository has a check that builds the site, and it would have caught that comma in a few seconds. It never got the chance, because the change went straight onto main and no pull request ran the check.

Right now, main accepts any change you send, even a quick browser edit on a Wednesday morning when you’re tired. On a team, the same rules are what make every change land through a review, by a person or, more and more often, of code an AI assistant wrote.

In this lesson, you’ll tell GitHub what main requires before it changes, test those rules with a push that should fail, and move the rejected commit to where it belongs. Start with the rules themselves and with what each one prevents.

Protect main With a Ruleset

To keep a broken commit off main, GitHub needs to know that main only changes through a pull request whose check passed. You set that up as a ruleset, a named set of rules that GitHub enforces on the branches it targets.

In older tutorials, you’ll also see GitHub’s branch protection rules, which predate rulesets. Rulesets do the same job and are what this course uses.

Your ruleset for main gets four rules:

  • Require a pull request before merging means that every change to main arrives through a pull request, so nobody can push a commit straight onto it, from the web editor or anywhere else.

  • Require status checks to pass means that a pull request can merge only after the status check named check is green. That name comes from the one job in check.yml, which is called check.

  • Block force pushes stops anyone from pushing a history that replaces commits already on main.

  • Restrict deletions stops anyone from deleting main on GitHub.

On a team, the pull request rule usually comes with at least one required approval, so a second person has read every change before it reaches main. That’s the kind of review you gave the turkey page’s draft in the last course, which caught a package nobody needed and a real-looking key.

You work alone on this repository, though, and GitHub doesn’t let you approve your own pull request, so a required approval would block every merge. Your ruleset requires zero approvals, and the mentor stands in for the second pair of eyes.

Open your generic-grocery repository on GitHub and create the ruleset:

  1. Click Settings, then Rules and Rulesets in the left sidebar.

  2. Click New ruleset, and choose New branch ruleset.

  3. Under Ruleset name, type Protect main.

  4. Set Enforcement status to Active. A new ruleset starts out disabled, so it doesn’t enforce anything until you activate it.

  5. Leave the Bypass list empty.

  6. Under Target branches, click Add a target and choose to include the default branch, which is main.

You might expect that, as the owner of the repository, you’d always be able to push to it anyway. With the older branch protection rules, that’s how it works by default, because admins skip them unless you turn on an extra option. A ruleset applies to everyone who isn’t on its bypass list instead. With the list empty, the rules bind you too, so you can’t skip them on a busy morning.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.