LoginSignup
9
6

More than 5 years have passed since last update.

ローカルでmasterでのcommitを禁止する

Last updated at Posted at 2019-03-15

やる理由

  • masterでcommitしてpushしようとするとGithubのmasterブランチの保護のおかげでエラーになる
  • そのあとにtopic branchを作成してmasterブランチではresetするのはめんどい
  • そもそもmasterにcommitできないようにしたい

やりかた

$cd path/to/project
$vim .git/hooks/pre-commit

printf "\e[31m%s\n\e[m" "[Error]"[Error] を赤文字で出力できる。

.git/hooks/pre-commit
#!/bin/sh

# if the branch is master, then fail.

branch="$(git symbolic-ref HEAD 2>/dev/null)" || \
       "$(git describe --contains --all HEAD)"

if [ "${branch##refs/heads/}" = "master" ]; then
    printf "\e[31m%s\n\e[m" "[Error]"
    echo "can't commit on master branch."
    echo "please commit on topic branch."
    exit 1
fi

masterにcommitしてみる

$git commit -m "masterにcommitしてみる"
can't commit on master branch.
please commit on topic branch.

成功

9
6
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
9
6