# git Branch 保護の設定 手順書 ## ブランチ保護設定手順 ==PC購入時に初期設定で行うこと推奨== ``` # global なgit hooks 設定用 ディレクトリを作る $ mkdir -p ~/.git_template/hooks # git hooks に設定を追記(master と releaseがつくbranch名全てpushできない) $ vi ~/.git_template/hooks/pre-push [vim] #!/bin/bash # pushを禁止するブランチ readonly MASTER='master' readonly RELEASE='^.*release.*$' # 「release」文字列を含む全てのブランチ while read local_ref local_sha1 remote_ref remote_sha1 do if [[ "${remote_ref##refs/heads/}" = $MASTER ]]; then echo -e "\033[0;32mDo not push to\033[m\033[1;34m master\033[m \033[0;32mbranch\033[m" exit 1 fi if [[ "${remote_ref##refs/heads/}" =~ $RELEASE ]]; then echo -e "\033[0;32mDo not push to\033[m\033[1;34m release\033[m \033[0;32mbranch\033[m" exit 1 fi done [vim] # pre-push に実行権限付与 $ chmod a+x ~/.git_template/hooks/pre-push # globalに設定する $ git config --global init.templatedir '~/.git_template' ``` 実装後は、クローンしたりして新しいリポジトリを入れた際にもpre-push のbranch保護設定が効いており、pushとブランチ削除ができないことを確認。 ### 既存のリポジトリに対して設定を聞かせる場合 branch 保護を効かせたいリポジトリ毎で以下の設定を行うこと ``` # pre-push を削除 $ rm .git/hooks/pre-push # 新しい pre-push を作成し、有効化 $ git init ``` ## 参考リンク [参考1](https://dev.classmethod.jp/articles/git-hook-pre-push/) [参考2](https://it-blue-collar-dairy.com/prohibit_push_to_master/)