Bitbucket Cloud branch restrictions
Branch restrictions are in the repository's Settings → Branch restrictions, under Workflow. Each one applies to a branch name or a pattern such as release/* and says who may write to it or merge into it through pull requests, whether its history may be rewritten or the branch deleted, and which merge checks its pull requests need (Use branch permissions). Below are the problems Atlassian's knowledge base answers about them.
A group or a person is not in the list
- A group: only groups with access to the repository can be added, and they need at least Write. Open the repository's Settings → Repository permissions, then Add users or groups (A user group is not appearing in branch restriction search results). To have a group on new repositories from the start, give it an access level at workspace level when you create it, or add it to the project (User group not available on Branch Restrictions).
- A person whose name has accents or umlauts: type the name with them. A search for "Zoe" does not find "Zoë" (Adding User Names with Acute Accents or Umlauts to Branch Restrictions).
To see the merge checks of each pull request before you merge: Pull Request Inbox for Bitbucket, a plugin for JetBrains IDEs, lists the pull requests that wait for your review and shows which checks pass and which block the merge.
"Internal server error" when you save one
In a repository with many branches, 500 or more, saving a branch restriction can run out of time and answer with a 500 error. The knowledge base's fix is fewer branches; it suggests keeping a repository under about 200 (Issue while updating branch restrictions). Merged branches are the easy ones to delete.
New branches: use a pattern
A new branch does not take over the restrictions of the branch it was made from. It gets the restrictions whose pattern its name matches, so a restriction on release/* covers every release branch from the moment it exists. Otherwise, create the restriction through the API once the branch is there, for example from a pipeline step that runs on every branch (Inherit branch restrictions for newly created branch from existing branch).
In a pattern, * matches zero or more characters and every other character matches itself: foo* matches foo and foobar but not barfoo, and * matches every branch. A restriction can also follow the repository's branching model instead, by branch type: production, development, bugfix, release, feature or hotfix (Branch restrictions in the REST API reference).
Allow only some branch names
To let people create only some branches, take write access and merging through pull requests away on *, then give them back on the names you allow. The knowledge base's example allows main and branches that start with test (Restrict Creation of Branches to Certain Patterns):
| Pattern | Write access and merging through pull requests |
|---|---|
* | No one |
main | The people or groups who may push to it |
test* | The people or groups who may create test branches |
A branch that matches both a name and * gets both restrictions. The same article shows what that enforces:
On * | On main | Who has access to main |
|---|---|---|
| No users or groups | User A | Only user A |
| Everybody | User A | Only user A |
| User A | User B | User A and user B |
| User A | No users or groups | Only user A |
| User A | Everybody | Only user A |
Through the API
POST /2.0/repositories/{workspace}/{repository}/branch-restrictions creates one, with an API token that has admin:repository:bitbucket. Each has a kind and a pattern. Only push and restrict_merges take users (by UUID, from the workspace members list) and groups: they are the ones let through, and an empty list stops everybody. To add people to an existing one, use PUT on its ID (the REST API reference; How to create/edit branch restrictions in Bitbucket Cloud repositories via API). For example, no direct pushes to release branches:
curl --request POST --user 'you@example.com:{api_token}' \
--header 'Content-Type: application/json' \
--data '{"pattern": "release/*", "kind": "push", "users": [], "groups": []}' \
https://api.bitbucket.org/2.0/repositories/{workspace}/{repository}/branch-restrictions
| On the settings page | kind |
|---|---|
| Write access | push |
| Merge via pull requests | restrict_merges |
| Minimum number of approvals | require_approvals_to_merge, with value |
| Minimum number of approvals from default reviewers | require_default_reviewer_approvals_to_merge, with value |
| Minimum number of successful builds for the last commit with no failed builds and no in progress builds | require_passing_builds_to_merge, with value |
| Maximum number of commits behind destination branch | require_commits_behind, with value |
| No unresolved pull request tasks | require_tasks_to_be_completed |
| No changes are requested | require_no_changes_requested |
| Reset approvals when the source branch is modified | reset_pullrequest_approvals_on_change |
| Keep approvals if there is no change to the diff in the pull request | smart_reset_pullrequest_approvals |
| Reset requested changes when the source branch is modified | reset_pullrequest_changes_requested_on_change |
| Prevent a merge with unresolved merge checks | enforce_merge_checks |
| Allow automatic merge when builds pass | allow_auto_merge_when_builds_pass |
Two more kinds, force and delete, go with Allow rewriting branch history and Allow deleting this branch.
Bitbucket pull requests in IntelliJ IDEA
Pull Request Inbox for Bitbucket, a plugin by Lesstep for JetBrains IDEs 2024.1 or later, lists the pull requests that wait for your review in an IDE tool window, with their diffs and discussions, and shows next to each which merge checks pass and which block the merge. The review inbox, diffs, discussions and merge checks are free; approving, commenting and merging are Pro, with a 30-day trial. It also works with Bitbucket Data Center (beta).
Get it on JetBrains Marketplace
Bitbucket and Atlassian are trademarks of Atlassian; IntelliJ IDEA is a trademark of JetBrains s.r.o. This page is not affiliated with or endorsed by Atlassian or JetBrains.
More: Bitbucket merge checks · "pre-receive hook declined" · Restore a deleted branch · Changes missing from a branch · 日本語