1

enter image description here

enter image description here

I have this code below in my Github Repository, i want to prevent anyone in my repo from committing directly to master, but it isnt working, what a, i doping wrong?

enter image description here

I have searched through stack overflow and youtube trying to find tutorials for this issue and i havent had any luck

9 lines (6 sloc)  154 Bytes
#!/bin/sh

branch="$(git rev-parse --abbrev-ref HEAD)"

if [ "$branch" = "master" ]; then
  echo "You can't commit directly to master branch"
  exit 1
fi

I want users to be blocked from committing directly to master Currently they are able to commit to master

My script is definitely execute-able, but still it doesnt display anything

Biffen
  • 6,249
  • 6
  • 28
  • 36
MurrackCarn
  • 21
  • 1
  • 7

1 Answers1

1

For such a hook to be locally executed, be sure to have your hook in a pre-commit file in .git/hooks

It will work for this particular repo, but should be introduced in each involved repo where you want this to be effective.


In addition to your hook you might want to configure github with branch protection rules so that even if someone commits on master locally, they will not be able to push those commits to github. You can even configure it to then require a pull request to get commits onto master. This is more important than your local hooks precisely because it's not something each team member can opt out of.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61