LoginSignup
0
0

More than 3 years have passed since last update.

Gitで特定のブランチへの直pushを禁止する

Posted at

Gitでmasterブランチに直pushを防ぐ

ローカルにていろいろファイルを作成して直pushを防止する

Git hooksとは

  • Gitでcommit、push、receiveなどのイベントの前後に実行するスクリプト。
  • Gitフックは組み込み機能で、ダウンロードの必要がない。
  • ローカルで実行される。

使ってみる

.git/hooksフォルダにスクリプトファイルを入れていくことで機能してくれるそう。
今回はpush時にスクリプトを走らせたいのでhttps://www.git-scm.com/docs/githooks#_pre_pushを見ていく。

pre-pushファイルを作成

# プロジェクトのルートでpre-pushを作成しスクリプトを記述する
vim .git/hooks/pre-push

# 実行権限「x」をつける
$ chmod +x pre-push

pre-pushの中身作成

こちらを参考にさせていただき、pre-pushの中身を作成
https://qiita.com/sensuikan1973/items/e6ab84403338a874b3aa

#!/bin/bash

# pushを禁止するブランチ
readonly MASTER='master'

while read local_ref local_sha1 remote_ref remote_sha1
do
  if [[ "${remote_ref##refs/heads/}" = $MASTER ]]; then
    echo -e "push禁止"
    exit 1
  fi
done

これで指定したブランチはローカルから直pushできないようになる

参考

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