How to block commits and pushes to a branch in Github
A step by step guide on creating branch protection rules that deny commits and pushes to specific branches in Github.
If you are looking to block commits to a local branch, we recommend that you follow the tutorial below.
Step One: Access the Branch Protection Rules
On Github, access the projects settings and select Branches from the left menu side bar.
Step Two: Add New Rule
Click the Add Branch Protection Rule button.
Step Three: Apply the Branch Name
Under Branch Name Pattern, add the name of the branch (i.e. main, uat, dev).
Step Four: Set Rules
Check off Require a pull request before merging and Require signed commits.
Step Five: No Bypasses
Check off Do not allow bypassing the above settings to make sure that no one, even administrators, can break these rules.
If you do not click this checkbox, administrators will be able to break the rules.
Step Six: Create Branch Protection Rule
Click the Create button.
Step Seven: Confirm
Enter your password and click the Confirm button.
This will complete the process, leading to a screen similar to the one below.
Step Eight: Test
In Terminal or on an application like Sourcetree, attempt to push a commit from the branch and confirm that it is denied.
Looking to prevent commits to a local branch ?
To prevent commits to specific local branches, you must use a pre commit githook script.
Read the article linked below to learn about the code that you must use.