How to prevent commits to a branch using a pre-commit githook script

Oscar de la Hera Gomez
A flower that represents Git with the text "Prevent Commits to Local Branches" beneath it.

To prevent commits from being allowed to specific local branches, add the code in this post to a pre-commit script in your projects githooks folder.

Copy the code below into your pre-commit script and replace BRANCH_NAME with the name of your branch (i.e. main or master).

If you want to disallow multiple branches, copy the code multiple times (i.e. one for dev, one for uat, one for main).

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details