force fail commit on master

Most of the time, you want to fail if you mistakenly commit to master. This is if you normally work with feature branch. This script can make git commit fail if the branch is master

add to .git/hooks/pre-commit

 current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
 if [ "$current_branch" == "master" ]
 then
   echo "Should not commit on master branch. Use 'git commit -n' to override"
   exit 1
 fi

or you can check out pre-commit a framework to manage your git commit scripts

Last updated