In this article, we will see how to lock master branch in GitHub repo and prevents users from pushing any changes to the branch. I am pretty sure whenever you plan your branching strategy, the first priority is always given to protect the master branch from any mishap as this branch is usually used for deployment in production as well as a reference for any feature addition or code changes.
GitHub is a famous git based repository hosting service used by millions of users around the world. It provides excellent web based interface to manage repositories and their branches. So any user who is using GitHub to manage their project code would first be looking to protect its repo master branch from any accidental commit by the developers or programmers working on that specific project. This can be easily done through a feature provided by the GitHub called
Branch Protection Rule. We will see the steps to lock the master branch by creating this rule in the below section.
How to lock master branch in GitHub Using 3 Easy Steps
Also Read: Solved: "error: failed to push some refs to remote"
Step 1: Prerequisites
a) You should have a valid GitHub Account.
b) You should have access to the Repo.
c) You should have access to create Branch Protection rule in the given repo.
Step 2: Login to GitHub
You need to first visit the GitHub login page and enter your valid credentials to login to your account as shown below.
Step 3: Lock Master Branch
After successful login, you need to go to your Repository as highlighted below. In our case, we are using a sample repository called
In the repository, you need to click on
Settings as highlighted below.
Then you need to go to
Branches section on the left side as shown below.
You will see an option called
Add branch protection rule under
Branch protection rules section. Click on it to proceed.
Here you need to provide
master branch under Branch name pattern.
Then check mark the
Lock branch option to prevent users from pushing changes to this branch. You can also check mark the
Allow fork-syncing option in case you want the branch to pull changes from its upstream repository. However it is important to note that this protection rule would not prevent
administrators or anyone with
bypass branch protections permissions from pushing any changes to
If you would like to prevent even
administrator or anyone with
bypass branch protections permissions to push any changes to
master then you need to check mark one more option called
Do not allow bypassing the above settings as well as you can see below.
Then you need to click on
Create to finally create this branch protection rule. It should look like below.
Now if you try to push any changes to the remote
master branch then you should end up with below output. This confirms that branch protection rule is working as expected and users are now not allowed to push to
cyberithub@ubuntu:~/atlantis-example$ git push -u origin master Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 314 bytes | 314.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), completed with 1 local object. remote: error: GH006: Protected branch update failed for refs/heads/master. remote: error: Cannot change this locked branch To git+ssh://github.com/cyberithub/atlantis-example.git ! [remote rejected] master -> master (protected branch hook declined) error: failed to push some refs to 'git+ssh://email@example.com/cyberithub/atlantis-example.git'
Hope the above article helped. Please let me know your feedback in the comment box !!