The protected branch feature restricts write and delete access to a specified branch. It also allows you to select users and teams to write to it using an allow list. Protected branch prevents unintentional changes to your Git repository. And it allows you to use your Git repository safely.
Restrictions when branch protection rules are applied
Restricted operations
In a protected branch, users are restricted to the following operations.
- Push
- Forced push
- Delete branch
An error message is returned when pushing to a protected branch.
Example of error message:
remote: You are not allowed to update protected branches on this repository.
Restricted options
Only certain users can push on a protected branch. The users and teams you specify when creating or editing a branch protection rule can push to the target branch.These users can force push if you have enabled force push in the branch protection rule options.
Note: There is no option to make protected branches deletable.
Icons indicating protected branches
Protected branches in the branch list screen are marked with an icon as follows:
Branch Protection Rules
Branch protection is set in the edit screen of the Git repository.
View rules
Rules set for branches in a repository can be viewed on the edit screen of each repository.
Add a rule
Follow these steps to add a branch protection rule:
- Open Edit on the Git Repository.
- Click on "Add rule” at the “Branch protection rules” section.
- Enter information in the pop-up window.
- Click "Add rule" to submit.
Name or pattern(Required)
Enter the name or pattern of the branch you wish to protect. Branch protection rules can be applied to a single branch or to all branches that match the pattern in the fnmatch syntax. For example, to protect only the main branch, enter main. To protect multiple branches containing the prefix release, use the * wildcard and type release*.
Users or Teams allowed to Push
You can allow only specific users or teams to push to a protected branch using an allow list. Search and select the users and teams you wish to allow to push in the select box. Only users and teams who have joined the project can be selected.
Allow Force Push
Enables forced push (push -f, push --force) on protected branches. Only users who are allowed to push by branch protection rules will be able to force push to a branch.
Edit a rule
Follow these steps to edit branch protection rules.
- Open Edit on the Git Repository.
- Click on the "Branch Name" of the rule you want to edit.
- Edit the information in the pop-up window.
- Click "Save changes" to submit.
Branches affected by the rule
Lists the branches to which the branch protection rule applies, matching the full text or pattern of the branch name entered.
Delete a protected branch
Follow these steps to remove a branch protection rule.
- Open Edit Git Repository
- Click on the delete icon "X" of the rule you want to delete
- Click "Delete" in the pop-up window