Agents never push to main — issue, branch, pull request
The flow
Section titled “The flow”- MUST start every agent change from an issue that states the intent, or link the existing issue. The issue is where a person agreed to the change; the pull request is where they check it.
- MUST work on a new branch for each issue, created from the current default branch and named with the issue number, for example
fix/22-bp-0014-ledgeroragent/22-bp-0014-ledger. - MUST NOT push, force-push or merge to the default branch or a release branch, and MUST NOT rewrite history on any branch another person or agent uses. This holds when the agent’s credentials would allow it and when a person asks for “just push it”: the agent pushes the branch and opens the pull request instead.
- MUST open a pull request against the default branch with a closing keyword for the issue (
Closes #22) in the description, and a summary of what changed, why, how it was verified (commands and results, BP-0059) and what is left. Closing keywords only take effect when the pull request targets the default branch. - SHOULD keep one issue per pull request and open it as a draft until its checks pass.
- MUST update the branch when the default branch moves (rebase or merge on the branch), resolve conflicts there and re-run the checks; never resolve them by pushing to the default branch.
- MUST leave approval and merge to a person. An agent MUST NOT approve or merge its own pull request, or another agent’s, and MUST NOT use a bypass even when its credentials hold one (BP-0069).
Clean up after the merge
Section titled “Clean up after the merge”- MUST clean up once the pull request is merged: delete the remote branch, delete the local branch and any worktree the agent created for it, prune stale remote-tracking refs, and switch the working copy back to the updated default branch. Turn on the repository setting that deletes head branches automatically after merge, so the remote side does not depend on the agent.
- MUST do the same when a pull request is closed without merging, after recording why on the issue; a branch nobody will merge is not a backup.
- MUST delete only what it created: branches with its prefix or named in its own pull request, never a branch another person or agent is using, and never with a force flag on a branch git says is not merged (
git branch -d, not-D), unless the pull request was squash- or rebase-merged and the agent has checked the pull request is merged.
Enforce it in the repository, not only in the prompt
Section titled “Enforce it in the repository, not only in the prompt”- MUST protect the default branch with a ruleset (or branch protection): require a pull request, require the status checks, block force pushes and deletion, and keep the bypass list empty or limited to named people for break-glass (BP-0075 rule 14).
- MUST give agents their own identity (a GitHub App or bot account) and credentials scoped to the repository and to pushing branches, never an administrator’s token. With a separate identity, the owner’s review counts as an independent approval, and the audit log shows which changes an agent made (BP-0032).
- SHOULD restrict where an agent can push to its own branch prefix, as hosted coding agents do, and add a local guard (a
pre-pushhook or an agent hook that blocksgit pushto the default branch, BP-0021). Local guards can be skipped (--no-verify); they catch mistakes, the ruleset is the control. - MUST, when the agent cannot reach the remote, hand over the branch (a bundle, a patch series or a pushed fork) and the pull request text, not an updated default branch for a person to push.
When to apply
Section titled “When to apply”Every repository an agent can write to: product code, infrastructure, documentation, this corpus. It applies to agents running locally with a person’s credentials as much as to hosted coding agents.
Examples
Section titled “Examples”Ruleset for the default branch (gh api -X POST repos/OWNER/REPO/rulesets --input protect-default.json). Set required_approving_review_count to 1 or more once agents have their own identity; a solo maintainer reviewing an agent that pushes as them cannot approve their own pull request.
{ "name": "protect-default-branch", "target": "branch", "enforcement": "active", "conditions": { "ref_name": { "include": ["~DEFAULT_BRANCH"], "exclude": [] } }, "bypass_actors": [], "rules": [ { "type": "deletion" }, { "type": "non_fast_forward" }, { "type": "pull_request", "parameters": { "required_approving_review_count": 1, "dismiss_stale_reviews_on_push": true, "require_code_owner_review": true, "require_last_push_approval": true, "required_review_thread_resolution": true } }, { "type": "required_status_checks", "parameters": { "strict_required_status_checks_policy": true, "required_status_checks": [{ "context": "build" }] } } ]}The agent’s side of the flow:
git switch -c fix/22-bp-0014-ledger origin/main# ...change, then run the project's checks...git push -u origin fix/22-bp-0014-ledgergh pr create --draft --base main --title "BP-0014: say where the gap ledger lives" \ --body "Closes #22. Changed: rule 5 names the ledger's location. Verified: bun run verify (187 pass). Left: none."
# after a person merges itgh pr view fix/22-bp-0014-ledger --json state --jq .state # expect MERGEDgit switch main && git pull --ff-onlygit push origin --delete fix/22-bp-0014-ledger 2>/dev/null || true # already gone if auto-delete is ongit branch -D fix/22-bp-0014-ledger # -D only because the PR state is MERGED (squash merges look unmerged to git)git fetch --pruneLocal guard (.githooks/pre-push, enabled with git config core.hooksPath .githooks):
#!/bin/sh# author: Stephen Eaton# .githooks/pre-push — refuse pushes to the default branch from this clone; changes go through a pull request.protected="refs/heads/main refs/heads/master"while read -r local_ref local_sha remote_ref remote_sha; do for p in $protected; do if [ "$remote_ref" = "$p" ]; then echo "pre-push: refusing to push to ${remote_ref#refs/heads/}. Push a branch and open a pull request." >&2 exit 1 fi donedoneexit 0Tested against a local bare repository: a push to main is refused with the message above, a push to fix/22-x succeeds, and git push --no-verify to main succeeds, which is why rule 11 puts the control on the server. The cleanup commands were run after a squash merge: git branch -d refused the branch as “not fully merged”, and -D removed it once the merge was confirmed.
Anti-patterns
Section titled “Anti-patterns”- “Small fix, pushed straight to main” by an agent, or by a person pushing what an agent prepared on main.
- An agent running with the owner’s administrator token, so the ruleset’s bypass applies to it.
- An agent approving or merging its own pull request, or two agents approving each other’s.
- Resolving a conflict by force-pushing the default branch.
- A pull request with no linked issue and a description written from the agent’s memory instead of the diff and the check output.
- Relying on “never push to main” in AGENTS.md with no ruleset behind it.
- Merged branches and agent worktrees left behind until nobody knows which are live.
Checklist
Section titled “Checklist”- Issue first; branch per issue, named with its number
- Pull request to the default branch with
Closes #N, verification and what is left - Default branch ruleset: pull request required, checks required, no force push or deletion, no agent on the bypass list
- Agents use their own identity with scoped credentials; they cannot approve or merge
- Local pre-push or agent hook blocks pushes to the default branch
- After merge or close: remote and local branch, worktree and stale refs removed; auto-delete head branches on
- Offline agents hand over a branch and pull request text, not a new main