会社アカウント等での個人 git への誤 commit 防止

#!/bin/bash

DIR=$(pwd)
EMAIL=$(git config user.email)

if [[ "$DIR" =~ ^/home/hogehoge/private/ ]]
then
    if [[ ! "$EMAIL" =~ @users.noreply.github.com$ ]]
    then
        echo "Error: git config user.email = $EMAIL"
        exit 1
    fi
fi
  • ファイル実行権限をつける (chmod 755)
  • git のテンプレートに設定する。git init 等で自動的に .git/hooks にコピーされる。
git config --global init.templatedir '~/.git_template'
  • /home/hogehoge/private/ 以下にあるフォルダで git commit する場合、その git の user.email が @users.noreplay.github.com を含んでなければエラーとなる。 (フォルダと email の規則は例です)