Skip to content
Versora

Manual

Checks

Protected branches

5 min read

On this page

In many teams, main (and sometimes develop and release/*) only receives changes through a merge request. Versora helps you avoid committing directly to these branches by accident: before the commit, it warns you and offers to move the work to a new branch; before the push, it warns you about the direct push and treats a force push to a protected branch as dangerous.

Which branches are protected

The app combines three lists:

  1. The repository's list, in Preferences › Checks › In this repository › Protected branches for this repository. Separate the names with commas; * works as a wildcard (release/*). When it's empty, the default applies: main, master and, with Gitflow, its production and development branches.
  2. The team's list, branches.protected in the repository's .versora.yml.
  3. The company's list, protected_branches in the machine policy.

On push, when it can be queried, the protected branch list from GitLab or GitHub itself applies (with the token from Git servers), and it takes precedence over the repository's list. Without a token, or with a remote that isn't a GitLab or GitHub one, the repository's list applies. If the lookup fails (network, slow server), the warning says "Couldn't query the server (…); the local list was used."

Direct commit to a protected branch

When it appears

When you commit with a protected branch checked out, the Before committing dialog shows the Protected branch block: "You're committing directly to main, which is a protected branch. Changes to it usually go through their own branch and a merge request."

When the branch is protected by the team or the company, a note says where the rule came from: "Protected by a team rule (.versora.yml): main." or "Protected by your company's policy: main."

Step by step: moving the commit to a new branch

  1. In the field of the Protected branch block, type the name of the new branch. With Gitflow, the field already comes with the feature prefix (for example, feature/). Spaces become hyphens.
  2. Click Create branch and commit there.
  3. The app creates the branch from the current branch and checks it out, taking along what is and what isn't staged. The block changes to "Created branch feature/ajuste-do-login from main: the commit goes there."
  4. If no other finding is pending, the commit happens right after, on the new branch.

If you'd rather commit to the protected branch anyway, use Commit anyway. Back cancels the commit.

Amend on a protected branch

When you amend the last commit of a protected branch, the warning is: "You're about to amend the last commit on main, which is a protected branch. Changes to it usually go through their own branch and a merge request." In this case there's no field for a new branch.

What Versora does in git

bash
git checkout -b feature/ajuste-do-login
git commit …

checkout -b takes along the staged and unstaged changes: the commit goes to the new branch, and the local main stays as it was.

How to undo

Ctrl+Z undoes the commit and then the branch creation ("Create branch feature/ajuste-do-login"), going back to the protected branch.

Push to a protected branch

When it appears

In the Before pushing dialog:

  • regular push: "You're about to push directly to origin/main, which is protected on GitLab." (or "which is protected by this repository's list");
  • force push, as dangerous: "You're about to overwrite origin/main, which is protected on GitLab. This rewrites history that other people have already pulled."

When the server reports how the branch is protected, the block adds:

  • "GitLab doesn't allow force pushes to this branch: the push will likely be rejected.";
  • "On GitLab, nobody can push directly to this branch: the push will likely be rejected. Push to another branch and open a merge request."

Push anyway continues; Back cancels.

Block by the team or the company

With branches.protected_mode: block in .versora.yml, a direct commit and push to a protected branch are rejected, without the "anyway" option:

  • on commit: the Protected branch dialog says "main is protected and doesn't accept direct commits (team rule (.versora.yml)). Create a branch for your work and open a merge request." and the Got it button cancels the commit;
  • on push: "origin/main is protected and doesn't accept direct pushes (team rule (.versora.yml)). Push your branch and open a merge request."

With protected_mode: warn, the usual warning applies, with confirmation.

When there are branches protected by the team or the company, the check applies even if it's turned off in Preferences or in the repository.

OptionWhat it doesDefault
Direct commit › Warn when committing to a protected branchWarns and offers to create a new branch with the changes.on
Push › Warn when pushing to a protected branchA force push to a protected branch is shown as dangerous.on
Ask the server › Ask GitLab/GitHubOn push, uses the server's protected branch list.on
Protected branches for this repositoryThe local list, comma-separated, with *.main, master (and the Gitflow ones)
Checks enabled here › Protected branchesTurns it off only in this repository (except with a team or company rule).on

Caveats and messages

  • "Enter the branch name.", "Invalid branch name." and "A branch named feature/x already exists.": adjust the name in the field.
  • "Couldn't create branch feature/x (see the notification).": the creation failed; the reason is in the error notification.
  • "The protected branch check failed: …": the app tells you and goes on with the operation.
  • "Couldn't query the server (Protected branches lookup: no response in 6 s); the local list was used.": the server took more than 6 seconds; the repository's list applied.
  • Preferences › Checks: the warnings and the repository's list.
  • .versora.yml: branches.protected and branches.protected_mode (warn or block). See Administrators.
  • Company policy: protected_branches adds branches to the list. Combining the sources means adding the lists together.

See also