masterではcommit出来ないようにしたい
- git hookで設定する
- $project/.git/hooks/pre-commit におくと、そのプロジェクトのみで有効。
- 下のように templatedirを設定しておくと、今後git cloneしたプロジェクトではすべて有効。
git config --global init.templatedir '~/.git_template'
-
~/.git_template/hooks/pre-commitor$project/.git/hooks/pre-commitに置くファイル
# ブランチがmasterならば
branch="$(git symbolic-ref HEAD 2>/dev/null)" || "$(git describe --contains --all HEAD)"
if [ "${branch##refs/heads/}" = "master" ]; then
# コミット履歴があるならばmasterへのCommitはfail
# コミット履歴がないならばmasterへのFirst Commitだけは許す
git rev-list --max-parents=0 HEAD > /dev/null 2>&1
if [ "$?" = "0" ]; then
echo "Do not commit on the master branch!"
exit 1
fi
fi
- 実行には権限追加が必要。
chmod 744 .git/hooks/pre-commit
- 実際にmasterブランチでcommitしてみる...OK!
git commit -av
# => Do not commit on the master branch!