3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

masterではgit commit出来ないようにhooks/pre-commitを設定する

3
Last updated at Posted at 2018-10-24

masterではcommit出来ないようにしたい

  • git hookで設定する
  • $project/.git/hooks/pre-commit におくと、そのプロジェクトのみで有効。
  • 下のように templatedirを設定しておくと、今後git cloneしたプロジェクトではすべて有効。
git config --global init.templatedir '~/.git_template' 
  • ~/.git_template/hooks/pre-commit or $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!
3
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?