git config --global init.templatedir '~/.git_template' mkdir -p ~/.git_template/hooks cd ~/.git_template/hooks vim pre-push
#!/usr/bin/env bash
remote="$1"
url="$2"
IFS=' '
allow=$(git config prevent-push-master.allow)
for pattern in $allow
do
if [[ $url =~ $pattern ]]; then
exit 0
fi
done
read local_ref local_sha remote_ref remote_sha
if [[ $remote_ref =~ 'master' ]]; then
echo 'Prevented push for master branch!'
exit 1
fi
chmod +x pre-push
これで git init や git clone した時に、.git/hooks にシンボリックリンクを貼ってくれるようになります。ただし、既存のリポジトリへは、個別に git init を行っておく必要があります。
なお、例外的にmasterへのpushを許可したいリポジトリがある場合は、以下のようにホワイトリストに追加します。
git config --global prevent-push-master.allow 'repo1 repo2'


