git config --global init.templatedir '~/.git-templates'
- so git will copy all templates into new or cloned repository
mkdir -p ~/.git-templates/hooks
touch ~/.git-templates/hooks/pre-commit
chmod +x ~/.git-templates/hooks/pre-commit
- Update
~/.git-templates/hooks/pre-commit
with the following content
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
MASTER_CHECK_ENABLED="$(git config --get custom.hooks.mastercheck || echo 'true')"
if [ "$BRANCH" = "master" ] && [ "$MASTER_CHECK_ENABLED" != "false" ]; then
echo "\033[0;31mCommit failed!"
echo "You can't commit directly to master branch.\033[0m"
echo "Run \`git config custom.hooks.mastercheck false\` to disable the check for current repo."
exit 1
fi
- Run
git init
in all repos you have already cloned. Done!
This will not work if particular repo already has pre-commit
hook. In that case just drop in bash part into existing script.