Select Page

Introduction

You’ve worked hard with your code and are now ready to push the files on GitLab, but only to get back an error message stating “you are not allowed to push code to protected branches on this project”. Why is this? 

Before everything else, what you need to know is that GitLab has several different levels of user permissions. The highest of all being Master, then comes the Developer, followed by Reporter and Guest users respectively. 

The guest and the reporter cannot push any code to protected branches in a GitLab repository. On the other hand, a Developer can only merge their files to a protected branch, but not directly push to it. And lastly, the Master is able to push code directly without any restrictions to a protected branch. If you’re unable to push code to the repository even after you have the Master permission, there could be several reasons for this. In this article, we’ll see how to get your way round this issue if you’re unable to push code even after having the Master permission, or how to set it if you haven’t already!


Why am I seeing the error message?

The reason for this error message is not having the required user permission to push files to the target branch. By default, the Master or the Main branch is protected, and only the Master user is allowed to push code on that branch. If you’re using a Developer user, or one with lower privileges, then you’d not be able to push code and will see that annoying error message. To bypass this issue, you must first enter the repository as a Master user, and access the Project Settings where you can disable the branch from being protected. 

The next section will discuss how to unprotect the given branch and allow users other than Master to allow pushing code to the branch.


Disable protection in branch

To unprotect a branch in GitLab, first login to your account and navigate to the repository under which your target branch is protected.

Once you’re in, click on Settings.

Scroll down on the sidebar and then click on Repository.

Navigate to the Protected Branches option. Here, what you’d want to do is click in Expand.

Now play close attention to this one, as this is the most important part of the whole process. In the above image, what you should already be able to tell is that the Main branch of my Demo repository is protected. I didn’t set it for protection myself, but rather, that is done by default. To now unprotect the branch and allow any level user to push code, click on the Unprotect button. You’d be prompted to make sure that you want to unprotect it. Simply click on ‘unprotect branch’, and you’re good to go. However, you should still be well aware of the security concerns it might raise. 

If you wish that only the developers and the repository Maintainers should be allowed to push code instead of all the users, it’s best you don’t unprotect it, as there’s a better option.

Just a little left to the unprotected button, you’d find a drop-down menu called “Allowed to push”. Click on it and select “Developers + Maintainers”.

This way, only the Developers and Maintainer/Master Level users will be able to push code to our repository.

If you’ve already unprotected the branch, then you can still navigate to that same Protected Branches option, select the branch you want to protect using the drop-down menu, and then click on Protect. You can also choose a different branch that is not protected by default if you want.

Nevertheless, there’s one other way you could push to the protected branch without needing to unprotect it. This can be achieved by using the force push method, and I’ll show you just that  in the next section.


Force push to protected branch

This method will allow any user who uses the –force flag when executing the git push command to push code even if the branch is protected. to enable this flag, once again visit the same option, called Protected branches.

Click on the toggle button under “Allowed to force push”. This will enable the –force flag to be used. 

Now try using the –force flag with the git push command with a lower level user to see if you can push:

$ git push <remote> <branch_name> --force

This method is not at all ideal and should not be used until wholly necessary. Using it will make your repository prone to security breaches, so take healthy precautions before you decide whether to use it. Also, note that only the Maintainer/Master user will be able to enable this option, so if you don’t have administrative access to the repository yourself, you’d need to ask your repository’s maintainer to enable the setting for you, given that they will agree to do so. 

This concludes our solution to the error message you may encounter regarding the protected branches. The next time you need to push code to some other branch and you see the error message, make sure to follow any of the solutions that apply, as long as you’re aware of the concerns it could raise!