⚙️

【GitHub】保護ブランチへの直pushを禁止&レビュー必須にしよう

2025/01/04に公開

「main ブランチに push しちゃった。。。」

とか

「レビューされてないのに PR マージしちゃったよ。。」

といったミスをしたことがある方がいるのではないでしょうか?

いくら気を付けていても人間なのでミスはします。

なので、システム的に防げるように

GitHub で保護ブランチへの直 push を禁止&レビュー必須を設定していきます

GitHub Free のプライベートリポジトリには適用されないためご注意ください!!

公式ドキュメント

・保護ブランチについて(⇒ 設定できるルールについて書かれています)

https://docs.github.com/ja/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches

・ブランチ保護ルールを管理する(⇒ 設定方法について書かれています)

https://docs.github.com/ja/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/managing-a-branch-protection-rule

設定要件

mainブランチに対して以下のブランチ保護ルールを設定します

  • マージ前に Pull Request のレビュー必須
  • 最低1人の Approve がないとマージできない
  • 追加コミットがあった場合は再度レビュー必須
  • 管理者に対しても有効
  • 強制 push と削除を無効

設定してみる

GitHub で設定したリポジトリのページを開き、

Settings -> Branches -> Branch protection rulesに遷移します

Add branch protection rule」をクリックしてブランチ保護ルールを設定します

main ブランチに対してブランチ保護ルールを適用する

Branch name patternmainと入力します

マージ前に Pull Request のレビュー必須

Require a pull request before mergingにチェックを入れます

最低1人の Approve がないとマージできない

Require approvalsにチェックを入れ、

Required number of approvals before merging を 1にします

追加コミットがあった場合は再度レビュー必須

Dismiss stale pull request approvals when new commits are pushedにチェック

管理者に対しても有効

Do not allow bypassing the above settingsにチェックをいれる

強制 push と削除を無効

Allow force pushes と Allow deletionsチェックを入れない

デフォルトでチェックは入っていません

最後に「Create」をクリックしてルールを作成します

動作確認

GitHub Free のプライベートリポジトリには適用されないためご注意ください!!

空のコミットをプッシュして動作確認してみます

// mainブランチか確認
$ git branch
* main

// 空のコミットを作成
$ git commit --allow-empty -m "test_commit"
[main ff95cf8] test_commit

// mainブランチにpush
$ git push
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 187 bytes | 15.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
remote: error: GH006: Protected branch update failed for refs/heads/main.
remote: error: Changes must be made through a pull request.
To https://github.com/*******/**********************.git
 ! [remote rejected] main -> main (protected branch hook declined)
error: failed to push some refs to 'https://github.com/*******/**********************.git'
// ↑のようにエラーになりpushできない

// 後片付け:空のコミットを取り消しておく
$ git reset --soft HEAD^

次に PR の方を確認していきます

ブランチをきって、push します

$ git checkout -b test_commit
Switched to a new branch 'test_commit'
$ git commit --allow-empty -m "test_commit"
[test_commit 6a683e2] test_commit
$ git push origin test_commit
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 187 bytes | 17.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'test_commit' on GitHub by visiting:
remote:      https://github.com/******/***************/pull/new/test_commit
remote:
To https://github.com/******/***************.git
 * [new branch]      test_commit -> test_commit

PR を作ってみると、下記のように 「Merge Pull Request」がクリックできなくなっています

参考記事

https://qiita.com/daijinload/items/8d990a6d1cbf07069e49

https://dev.classmethod.jp/articles/github-main-branch-protection/

https://zakkuri.life/github-protect-branch/

https://qiita.com/katsukii/items/5368598cbecbaefd1ed8

Discussion