How do i protect my main branch from direct pushes? #195760
Replies: 2 comments
-
|
Two ways to do this — the classic branch protection rule or the newer rulesets. Both work, rulesets are now the recommended path. Option 1 — Rulesets (recommended)
Save. Direct pushes to Option 2 — Branch protection (classic) Repo → Settings → Branches → Add branch protection rule → branch name pattern Heads-up if you're the repo owner / admin By default, admins can still bypass these rules. In rulesets there's a Bypass list — leave it empty if you really want to lock yourself out of direct pushes. In classic protection, tick Do not allow bypassing the above settings. If you're on a free plan with a private repo, branch protection used to be Pro-only but it's been free for everyone for a while now, so this should work on any repo. |
Beta Was this translation helpful? Give feedback.
-
|
You can protect your Steps:
Once this is set, GitHub will block any direct push to Optional extras worth enabling:
If you use the CLI, you can also set this with: gh api repos/{owner}/{repo}/branches/main/protection \
--method PUT \
--field required_pull_request_reviews[required_approving_review_count]=1 \
--field enforce_admins=trueAfter this, you'll need to work in feature branches and merge via PR — a great habit that also protects your commit history! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
🏷️ Discussion Type
Question
💬 Feature/Topic Area
ARC (Actions Runner Controller)
Discussion Details
i keep accidentally pushing directly to main, how do i set it up so that it requires a pull request before merging?
Beta Was this translation helpful? Give feedback.
All reactions