restrictions on branch creation
Asked by Max DeCurtins:

Is there any way to restrict branch creation if a branch already exists matching some pattern? Or is there a way to enforce a limit on the number of branches with a certain prefix, e.g. \`release/\`, such that there can only be one branch with that prefix at a time?

I would be looking for Bitbucket to enforce restrictions on branch creation within Bitbucket (regardless of which branch is the source), as well as applying them in the form of one or more hooks run when someone pushes a local branch to the remote.

I found this <https://community.atlassian.com/t5/Bitbucket-questions/Branch-Permissions-to-restrict-branch-creation/qaq-p/661315> but it's 5 years old and it's not clear that it's relevant to Bitbucket Cloud.


Answers:
author: Dam
body: Hi [@Max DeCurtins](/t5/user/viewprofilepage/user-id/4143354) ,

In Bitbucket Cloud, you can set branches permissions and branching model for each repository separately:

* The doc for permissions: <https://support.atlassian.com/bitbucket-cloud/docs/use-branch-permissions/>
* The doc for branching model: <https://support.atlassian.com/bitbucket-cloud/docs/branch-a-repository/#BranchingaRepository-branching_model>

I hope this helps.

Cheers,  
Dam.

comments: author: Max DeCurtins
body: [@Dam](/t5/user/viewprofilepage/user-id/722152) thanks for replying but did you read my question? The docs you linked don't provide any information pertinent to my question.


author: Dam
body: Of course I read your question... Your comment is not nice, stay calm and gentle please.

I was mentioning the doc of Bitbucket Cloud cause first a part of your need can be done on repository level directly on the repo config. Before looking at hooks and stuff around, have a look at what's possible to do directly in the repo settings.

The apps Jira Hooks for Bitbucket mentioned in <https://community.atlassian.com/t5/Bitbucket-questions/Branch-Permissions-to-restrict-branch-creation/qaq-p/661315> is not compatible for Bitbucket Cloud:   
[https://marketplace.atlassian.com/apps/1211959/jira-hooks-for-bitbucket?hosting=server\&tab=overview](https://marketplace.atlassian.com/apps/1211959/jira-hooks-for-bitbucket?hosting=server&tab=overview)

Again branch creations based on patterns and permissions can be done on each repo with Bitbucket Cloud features.

There is even more doc here: <https://bitbucket.org/blog/take-control-with-branch-restrictions>. Enforcing a limit on the number of branches with a certain pattern is apparently not possible but you can limit the number of users allowed to create them which will limit the possible branch creator by pattern.

Cheers,  
Dam.


author: Max DeCurtins
body: My apologies, I had no intention to be rude. I did, however, ask a specific question. I have spent a good amount of time in Repository Settings and I'm familiar with the options available there.

Your comment: "Enforcing a limit on the number of branches with a certain pattern is apparently not possible but you can limit the number of users allowed to create them which will limit the possible branch creator by pattern." This addresses my question, thank you. I'm seeking to limit the number of branches with a certain pattern, and it is, as you say, likely not possible. I have no need to limit who is allowed to create branches because we are all co-equal devs on my team and anyone is allowed to create any type of branch; what I am seeking to do is implement some automated guards to make sure that we are all more careful about adhering to particular workflows.


author: Dam
body: \> *I am seeking to do is implement some automated guards to make sure that we are all more careful about adhering to particular workflows.*

I see better now what you need also... I think you can find a possible solution with git hooks... and Bitbucket support git hooks. There is a nice doc about that here: <https://www.atlassian.com/git/tutorials/git-hooks>

Of course you will have to code the hook according to your needs, but there is `pre-receive` hook that can help...

Again I hope this helps.

It's an interesting topic :)

Cheers,  
Dam.


Tags: bitbucket-cloud

cloud