Documentation

gitHub

Branch protection

Most of the repository hubs provide the ability for repository administrators to protect branches. The most common use case for protecting a branch is to prevent its deletion and to force all contributions to that branch to be peer reviewed before being integrated.

 

Protections only apply to branches within the remote repository. In your own local clone, you are free to checkout a protected branch, modify it and even delete it, since these operations are local and do not affect the remote repository. 

 

However, pushing changes to a protected branch will most likely fail. So what if you end up in that situation? What should you do in case you have local commits that you cannot push due to branch protections?  What if you get an error message such as "TF402455: Pushes to this branch are not permitted; you must use a pull request to update this branch."

 

Workgroup - branch protection error

 

You should follow all 5 steps below:

1) Start by creating a new branch from the top bar. That branch will include the commits that you were not allowed to push from the previous branch.

Workgroup - branch protection step 1

 

2) Push your local commits. This time, they will not be rejected because you will be pushing your newly created branch instead of targeting the protected branch.

Workgroup - branch protection step 2

 

 

3) Submit your changes for review by opening a change request. Make sure to choose the protected branch as target branch.

Workgroup - branch protection step 3

 

At this stage, your local copy of the protected branch is not par with its remote counterpart, as it still contains the commit(s) that you could not push. 

 

4) To reset your local copy of the protected branch, you must delete it from the top bar. This will not affect the remote repository. Moreover, there is no risk to lose your contribution since you just pushed it.

Workgroup - branch protection step 4

 

5) Checkout a new local copy of the protected branch. The changes that you were not allowed to push will not be available in that branch as long as your change request has not been reviewed and merged by one of your teammates.

Workgroup - branch protection step 5

 

 

Related content