To let AI coding agents open pull requests safely on GitHub, protect your default branch with a ruleset or branch protection rule that requires a pull request, at least one approval from a code owner, passing status checks and fresh approval after every new push, and keep agents off the bypass list. These are standard GitHub settings, documented in GitHub’s guides to code owners and protected branches. They matter more now that agents write a growing share of code and tools such as Anthropic’s Claude Ship can even approve routine changes.

This guide walks through the setup in the order we recommend, explains why each setting exists, and ends with a worked example for a typical repository.

Step 1: Decide what an agent may and may not do

Before touching settings, write down three rules for your team. They will translate directly into configuration:

  1. Agents work on branches, never on the default branch. Every change reaches main through a pull request.
  2. Some paths always need a named person. Typical examples are authentication code, payment logic, infrastructure-as-code, CI workflows and dependency manifests.
  3. No one, human or bot, merges red builds. Tests and linters must pass first.

If you run several agents at once, give each its own branch and working directory, as described in our guide to running parallel coding agents with git worktrees. Clean branches make every later control easier to reason about.

Step 2: Write a CODEOWNERS file

A CODEOWNERS file maps paths to the people or teams responsible for them. GitHub looks for it in .github/, the repository root or docs/, in that order, and uses the first one it finds. Owners are automatically requested for review when a pull request touches their files (except drafts, which notify owners when marked ready).

Close-up of a screen showing lines of CSS code in an editor
CODEOWNERS uses gitignore-style path patterns, so ownership rules sit right next to the code they protect. Photo: Sai Kiran Anagani _imkiran / Wikimedia Commons, CC0

A few rules from GitHub’s documentation catch people out:

  • The last matching pattern wins. Put broad defaults at the top and specific paths below them.
  • Multiple owners for one pattern must share one line. If you repeat a pattern on separate lines, only the last line counts.
  • Owners need write access. A user or team without explicit write permission is silently not assigned, and teams must be visible.
  • Paths are case-sensitive, even if your laptop’s file system is not.
  • Invalid lines are skipped, so a typo can quietly remove protection. GitHub highlights errors when you open the file in the web interface.
  • The base branch’s file is used. A pull request is governed by the CODEOWNERS file on the branch it targets, so an agent cannot weaken ownership in its own PR before it merges.

A starting point for a web service might look like this:

*                       @acme/maintainers
/src/auth/              @acme/security
/src/billing/           @acme/payments
/infra/                 @acme/platform
/.github/workflows/     @acme/platform @acme/security
/.github/CODEOWNERS     @acme/maintainers

The first line makes the maintainers team the default owner of everything. The next four give sensitive areas to specialist teams; because the last matching pattern wins, they override the default. CI workflows list two teams on one line, so either can approve.

The final line, which protects the ownership rules themselves, is the one teams forget. If an approval policy or ownership file can be changed by the same pull request it governs, it is not really a control. The same applies to any agent approval policy stored in the repository.

Step 3: Require reviews that cannot go stale

Next, protect the default branch. You can use a classic branch protection rule or a newer ruleset; both offer the same review options. Turn on:

  • Require a pull request before merging, with at least one approval.
  • Require review from Code Owners, so files listed in CODEOWNERS need an owner’s approval. Note that an approval from any one listed owner is enough.
  • Dismiss stale pull request approvals when new commits are pushed. GitHub records the diff at approval time; if it changes, the approval is dismissed.
  • Require approval of the most recent reviewable push. Someone other than the last pusher must approve the latest changes.

The last two settings close a real gap with agents. A fixing agent may keep pushing commits after a human approved an earlier version. GitHub’s documentation warns that if you worry about approved pull requests being “hijacked” with unreviewed content, dismissing stale reviews is the safer choice. With both settings on, a fix pushed by an agent always needs a fresh look.

Step 4: Require status checks from the right source

Under Require status checks to pass before merging, add your test, lint and build jobs. Three details matter:

  • Choose strict or loose. Strict mode, which GitHub uses by default, requires the branch to be up to date with the base before merging. It costs more CI runs but catches conflicts between parallel agent branches.
  • Pin the source of each check. Anyone with write access can set a status, so GitHub lets you pick the GitHub App expected to report each required check. Pin it, so a bot cannot mark its own work green.
  • Keep job names unique across workflows. Duplicate names make required checks ambiguous and can block merges.

If you adopt an AI reviewer that reports a pass or fail result, it can become one more required check. Anthropic, for example, recommends running Claude Approvals in observe-only mode first and only then making it required in branch protection.

Step 5: Keep agents off the bypass list

Rulesets let you name roles, teams or GitHub Apps that can bypass the rules. Classic branch protection, by default, does not apply to admins unless you tick the option to include them. Check both:

  • Do not add coding agents or their GitHub Apps to any bypass list.
  • Include administrators in branch protection, or limit bypass to a small break-glass team.
  • Give agents the narrowest token possible, as covered in our guide to least-privilege API keys for AI coding agents. An agent that only needs to push branches and open PRs should not hold admin rights.

Optional extras from the ruleset menu add more assurance: require signed commits so every commit is verified, require linear history for easier reverts, and restrict deletions and force pushes on the default branch.

A worked example

Imagine a five-person team where an agent opens about 20 pull requests a week. With the setup above:

  1. The agent opens a PR that changes /src/billing/invoice.ts and a test file.
  2. CODEOWNERS requests review from @acme/payments; CI runs tests, lint and build.
  3. A payments engineer approves. The agent then pushes a small fix for a failing lint check.
  4. Because stale approvals are dismissed and the latest push needs approval, the PR is blocked again until someone reviews the fix.
  5. Once checks pass from the pinned app and the new approval lands, the PR can merge.

A docs-only PR touching /docs/ follows the same path but needs only a maintainer, so routine changes stay fast while billing code always gets a specialist.

What to watch next

  • Whether GitHub adds native options for policy-based or AI-assisted approvals in rulesets.
  • How AI review tools report results: as required checks, reviews or both.
  • New audit requirements for agent-approved changes in regulated industries.

Key takeaways

  • Route every agent change through a pull request on a protected default branch.
  • Use CODEOWNERS for sensitive paths, and make the CODEOWNERS file itself owned.
  • Dismiss stale approvals and require approval of the latest push, so agent fixes are always re-reviewed.
  • Pin required status checks to the app that should report them, and keep agents off bypass lists.
  • Start any AI approver in observe-only mode before making it a required check.

Frequently asked questions

What is the difference between rulesets and branch protection rules?

Both protect branches with the same core requirements, such as required reviews and status checks. Rulesets are the newer system: several can apply at once, they offer bypass lists for roles, teams and apps, and they cover tags as well as branches.

Can a pull request change its own CODEOWNERS rules?

Not for itself. GitHub uses the CODEOWNERS file on the base branch of the pull request, so edits in the PR only take effect after it merges, which is why the file should have its own owner.

Should an AI agent ever approve pull requests?

That is a team policy decision. If you allow it, limit it to low-risk paths through a policy file that people own, keep human review for sensitive code, and start in observe-only mode.