LoginSignup
0
0

gitで変更に特定の文字列を含んでいた場合はコミットできないようにする

Posted at

自分のデバッグ用に一時的に変更した箇所等を誤ってコミットしてしまわないように、変更に特定の文字列を含んでいたらエラーにする。

.git/hooks/pre-commitを以下の内容で作成。

#!/bin/bash

marker_message="DO_NOT_COMMIT"
echo "check marker message '$marker_message'"
git diff --cached|grep '^+.*'"$marker_message"
if [ ${PIPESTATUS[1]} -eq 0 ]; then
    echo "error. changes contain marker message '$marker_message'"
    exit 1
fi
echo "OK. changes does not contain marker message"

実行権限を付与

chmod a+x .git/hooks/pre-commit

これで以下のような変更を加えた場合にはコミットでエラーになる。

flag=xxx_hantei()
flag=True #DO_NOT_COMMIT 無理やりフラグを変える ←この行を追加した場合はコミットでエラーになる
if flag==True:
    
0
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
0
0