LoginSignup
7
3

More than 5 years have passed since last update.

git で remote master ブランチへのプッシュを禁止する

Last updated at Posted at 2017-08-04

設定にあたって、意外とあちこちから情報を集めたので、まとめます。

新たに clone されたリポジトリから master へプッシュできないようにする

フォルダをつくります

mkdir ~/.git_template/hooks

hook シェルを編集します

vi ~/.git_template/hooks/pre-push

~/.git_template/hooks/pre-push

#!/bin/bash

while read local_ref local_sha1 remote_ref remote_sha1
do
  if [[ "${remote_ref##refs/heads/}" = "master" ]]; then
    echo "Do not push to master branch!!!"
    exit 1
  fi
done

実行権限を付与します

chmod +x ~/.git_template/hooks/pre-push

git clone した際に、各リポジトリにコピーされるようにします

git config --global init.templatedir ~/.git_template/

既存のリポジトリでも master へプッシュできないようにする

設定をコピーします

cd hogerepo
cp ~/.git_template/hooks/pre-push ./git/hook/pre-push

挙動

master ブランチで push しようとすると、こんなエラーになります。

$ git push -u origin master
Do not push to master branch!!!
error: failed to push some refs to 'git@github.com:hoge/hogerepo.git'

もちろん、マージはできます!

注意点

各人が設定する必要があります。
誰かが設定してプッシュしとけばみんな同じ設定になるわけじゃないです。

参考情報

masterブランチにpushさせないようにするフック
master への push を禁止するローカル git hook の正しい書き方

7
3
1

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
7
3