1
0

More than 3 years have passed since last update.

git hooksのpre-commitでmasterにそもそもコミットさせないようにする

Last updated at Posted at 2020-06-17

課題

うっかりmasterにコミットしてしまう事故がよく発生するので、そもそもpre-commitでコミットする前の段階でエラーで弾くようにした。

前提

mac OS 10.15.4
windows10

準備

git/hooks配下に「pre-commit」ファイルを用意する

コード

git symbolic-ref HEAD --shortで現在のブランチ名を取得
masterと文字列比較をすることで、自分が今masterブランチ以外のブランチにいるということを確認している


#!/bin/sh
branch=`git symbolic-ref HEAD --short`
if [ ${branch} = master ]; then
  cat <<\EOF
  エラー:masterブランチにcommitはできません。
EOF
    exit 1
fi

参考

pre-commit hookでmasterへのcommitを禁止した

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0