Git の master/main への直接pushを防ぐ

誤って master/main へ直接pushしてしまわないようにクライアント側のhookで、特定のbranch名へのpushを禁止する

ref: https://git-scm.com/docs/githooks

プロジェクトごとの pre-push hook

プロジェクトごとの hook path は .git/hooks/ になっているので、ここにファイルを作成する。

#!/bin/bash
protected_branches=('main' 'master')
while read local_ref local_sha1 remote_ref remote_sha1
do
  if [[ "${protected_branches[@]}" =~ "${remote_ref##refs/heads/}" ]]; then
    echo "Do not push to master branch!!!"
    exit 1
  fi
done

(参考) hook の template を作る

init する際に、template をもとに、そのプロジェクトにhookが追加される

git config --global init.templatedir "~/.git_template/"
#!/bin/bash
protected_branches=('main' 'master')
while read local_ref local_sha1 remote_ref remote_sha1
do
  if [[ "${protected_branches[@]}" =~ "${remote_ref##refs/heads/}" ]]; then
    echo "Do not push to master branch!!!"
    exit 1
  fi
done

global の hookPath を設定する

全てのproject共通でhookさせたい場合は、globalで設定する

git config --global core.hooksPath "~.git_hooks"
#!/bin/bash
protected_branches=('main' 'master')
while read local_ref local_sha1 remote_ref remote_sha1
do
  if [[ "${protected_branches[@]}" =~ "${remote_ref##refs/heads/}" ]]; then
    echo "Do not push to master branch!!!"
    exit 1
  fi
done