How To Limit Pushing Changes To Specific Remote Branches in Bitbucket

On this post we are going to learn how to limit pushing changes to specific remote branches in Bitbucket.

Let’s say we have the following repository branches.

  • main or master
  • uat
  • qa

We do not want users to directly push their changes to any of the remote branches without going through pull request and code review.

To do this we use the bitbucket feature of branch permissions.

Go to Repository Settings > Under Workflow go to Branch Permissions > then click on Add a branch permission

Set the following permissions for the QA branch:

  • Selecting Branch – By name or pattern set to qa
  • Write access – none
  • Merge via pull request – the user or everybody
  • Merge checks = optional check for at least 1 approval – to be sure it got into code review.
  • Hit Save.

That should be it. Test it out

Clone your repository project and checkout the qa branch, make some local changes and try to push those changes via git push. You would get the error message.

! [remote rejected]   qa -> qa (pre-receive hook declined)

Now create a new branch and push that new branch to the remote. Then create a pull request from the new branch to the qa branch. Finally approve the Pull request and merge.

Repeat the same steps for setting branch permission for the uat and main branch.

Leave a Reply

Your email address will not be published.