Skip to content
TurnIntoCMS

How to protect the main branch on client websites

By Yash Garg · · 7 min read

To stop broken production builds and erased history on client websites, configure server-level protection rules on the main branch that simultaneously block force pushes and reject branch deletions. Setting these guards at the remote host guarantees that neither a rushed developer nor an automated script can overwrite production commits. According to GitHub's protected branches documentation, creating a branch protection rule automatically blocks force pushes and prevents the targeted branch from being deleted.

Relying on client-side Git hooks like pre-push scripts fails in real agency workflows. Centralized enforcement is the only reliable safeguard when multiple developers push changes. Standardizing these baselines as part of your process for client handoff for agencies removes human error before you transfer project control.

Why client repositories break without strict protection

Production incidents on client repositories rarely stem from bad code alone. More often, they happen when diverged Git history overwrites recent work. When a developer rebases a local branch against an older snapshot and runs a force push, Git replaces the remote branch pointer with the diverged commits.

A serious gap occurs when teams block force pushes but forget to restrict branch deletions. If someone has permission to delete branches, they can delete the remote main branch and push a new diverged branch with the same name, achieving the exact same destructive result. On bare Git remotes, setting `receive.denyNonFastForwards` and `receive.denyDeletes` to true in configuration closes both attack vectors at the protocol layer, as documented in the official Git configuration guide.

Branch deletions also trigger immediate production outages. Even though commit objects still exist in Git storage, deleting the branch reference severs active deployment webhooks and halts deployment pipelines.

Configuring repository rulesets on GitHub

GitHub repository rulesets have replaced legacy branch protection as the primary tool for repository governance. A single repository can run up to 75 rulesets concurrently, using fnmatch path matching syntax to apply layered protection patterns across branches, according to GitHub's ruleset documentation.

Agencies no longer need to manage these settings repository by repository. Organization-level rulesets allow administrators to apply consistent branch and tag protections across all or select repositories in an organization, a feature expanded to GitHub Team plans as reported in the GitHub Changelog on organization rulesets.

Follow these steps to configure a baseline ruleset for a production repository:

  1. Navigate to repository or organization settings, select Rules, and click New ruleset.
  2. Name the ruleset Protect Production and set enforcement to Active.
  3. Under Target branches, choose Add target and select Default branch.
  4. Under Branch rules, select Restrict deletions to prevent dropping the branch reference.
  5. Select Block force pushes to require fast-forward updates.
  6. Enable Require a pull request before merging and require at least one code owner approval.

The table below outlines how core protection settings correspond across Git platforms:

Protection SettingGitHub RulesetsGitLab Protected BranchesBare Git Server
Block Force PushesBlock force pushesAllowed to force push: Disabledreceive.denyNonFastForwards = true
Block Branch DeletionRestrict deletionsBuilt-in by default on protected branchesreceive.denyDeletes = true
Require ReviewRequire a pull requestMerge request approvals requiredServer-side pre-receive hook
Admin Bypass LockKeep bypass list emptyMaintainer push access disabledUnix file system permissions

Setting protection rules on GitLab and Bitbucket

GitLab activates default branch protections automatically whenever a new project is created, according to GitLab's protected branches documentation. Under these defaults, force pushes are set to nobody, and direct deletion over the Git protocol is blocked.

Each user can protect or unprotect branches up to 5,000 times every hour in each top-level group or personal namespace, according to the GitLab protected branches API documentation.

Understanding conflict resolution is essential when combining group rules with project rules on GitLab. According to the GitLab branch protection documentation, push and merge permissions follow the most permissive rule when multiple settings apply to the same branch. In contrast, Code Owner approval requirements resolve using the most restrictive rule, ensuring required reviews cannot be bypassed by a looser project-level permission.

On Bitbucket, open repository settings, choose Branch permissions, and select your production branch pattern. According to Atlassian's branch permission documentation, check both Prevent deletion and Prevent rewriting history, then enable Prevent changes without a pull request to route incoming commits through peer review.

Closing the administrator bypass trap

A common blind spot in legacy branch protection systems is the administrator bypass. In older GitHub settings, repository administrators bypassed branch protections automatically unless an explicit restriction toggle was checked. Because agency leads usually hold administrator status on client repositories, an accidental local push command could overwrite production history without warning.

When creating a ruleset, you can choose to allow specific roles, teams, or GitHub Apps to bypass the rules, as outlined in GitHub's ruleset documentation. Unless a user, team, or application is deliberately added to the bypass list, repository administrators must follow the exact same rules as standard contributors.

Industry security benchmarks support this approach. Security guidance from the OpenSSF project recommendations advises protecting the default branch against force pushes so that every change made to code can be retained and examined. If your deployment pipeline requires direct push access, grant scoped bypass rights to an automated deployment bot or a dedicated GitHub App rather than an individual developer.

Common branch protection mistakes and how to fix them

Setting up branch protections is straightforward, but subtle configuration oversights can leave your production branch exposed. Here are four frequent mistakes agencies make and how to resolve them:

  • Mistake: Blocking force pushes while leaving branch deletions permitted.

Fix: Ensure deletion restrictions are enabled alongside non-fast-forward push blocks. Otherwise, a collaborator with push access can delete the remote branch and recreate it with altered history.

  • Mistake: Relying on local pre-push Git hooks for repository security.

Fix: Enforce protections at the host or remote server level. Developers and contractors can easily bypass local hooks with command line flags.

  • Mistake: Permitting administrative bypasses for convenience.

Fix: Leave the ruleset bypass list empty. If emergency updates are needed, require a fast-track pull request rather than leaving permanent administrative exemptions open.

  • Mistake: Granting full administrator tokens to CI/CD pipelines.

Fix: Use scoped bot credentials or dedicated GitHub Apps with narrow permissions. Unchecked deployment credentials can accidentally wipe branch references during deployment failures.

Handling client handoffs and editing workflows

Transferring a repository to a client organization changes access permissions across the board. According to GitHub's repository transfer documentation, transferring a repository grants the new owner full administrative control over repository settings, while the original owner remains attached as a collaborator. Before handing over the repository, verify that your branch rulesets are active so the client cannot accidentally break production during early team onboarding.

Modern visual development tools and AI site builders introduce additional branching considerations. For instance, builders like Lovable sync project edits directly against a single branch, targeting the repository default branch unless configured otherwise, as noted in the Lovable GitHub integration guide. Connecting an AI builder directly to a strictly protected main branch causes sync failures when the service attempts to push automated commits without an approved pull request. To prevent broken sync workflows, establish a dedicated staging branch for AI builders and review our guide on branch strategy for client content.

Non-technical content editors should never need to push code from a command line or navigate Git merge conflicts to update site copy. For websites built with Next.js, React, or Vite, tools like TurnIntoCMS provide a visual editing layer for client updates. It connects directly to the GitHub repository, allowing clients to click and edit copy, pictures, and list items on a live preview. When the client publishes changes, TurnIntoCMS writes the updates back to the repository as a clean Git commit on a chosen branch without requiring the client to have a GitHub account.

Keep in mind that TurnIntoCMS works strictly with content stored directly in code or local data files, rather than external databases or runtime APIs, and is designed specifically for GitHub repositories. Using a dedicated visual editor ensures your main branch remains protected by server rules while clients update their content independently.

FAQ

How do I stop developers from force pushing to the main branch?

Configure server-side branch protections on your remote host by checking Block force pushes in GitHub Rulesets or setting force pushes to disabled in GitLab. These server-level rules reject non-fast-forward push commands before commits touch your remote repository.

What happens if someone deletes the main branch?

The remote reference pointer is removed, causing connected webhooks, continuous deployment pipelines, and preview builds to fail immediately. While commit objects remain temporarily in Git storage and can be restored using local reflogs, deleting the branch halts active deployments.

Can administrators bypass branch protection rules?

Under modern GitHub Rulesets, administrators cannot bypass rules unless they are explicitly added to the bypass list. Leaving the bypass list empty ensures that all developers and administrators must follow standard pull request and merge policies.

What is the difference between git push force and force with lease?

Standard force pushing overwrites remote history unconditionally, destroying any commits added to the branch since your last pull. The force-with-lease flag checks whether your local tracking branch matches the remote branch reference before overwriting, refusing the push if someone else pushed updates in the meantime.

Sources

  1. Git - Git Configuration (git-scm.com, checked November 19, 2026)
  2. Protected branches (docs.gitlab.com, checked November 19, 2026)
  3. Protection rules and permissions (docs.gitlab.com, checked November 19, 2026)
  4. Protected branches API (docs.gitlab.com, checked November 19, 2026)
  5. Using branch permissions (confluence.atlassian.com, checked November 19, 2026)
  6. Organization rulesets now available for GitHub Team plans (github.blog, checked November 19, 2026)
  7. Default Branch Should Not Allow Force Pushes (best.openssf.org, checked November 19, 2026)
  8. About rulesets (docs.github.com, checked November 19, 2026)
  9. Transferring a repository (docs.github.com, checked November 19, 2026)
  10. About protected branches (docs.github.com, checked November 19, 2026)
  11. Sync your Lovable project with GitHub (docs.lovable.dev, checked November 19, 2026)

Related

More from the blog

Let your clients edit the site you coded

One website free. Then $1 each, paid once.

Get started free