ブランチ保護ルール
この章では、mainブランチへの直接プッシュ禁止、PR必須化、必須レビュー数の設定など、ブランチ保護ルールの詳細を学びます。
ブランチ保護とは
Section titled “ブランチ保護とは”ブランチ保護は、重要なブランチ(通常はmain)に対して、特定のルールを強制する機能です。
保護する目的
Section titled “保護する目的”- コード品質の維持: レビューなしのマージを防止
- CI/CDの確保: テストが通らないコードをブロック
- 事故の防止: 誤った force push から保護
- コンプライアンス: 監査要件への対応
- リポジトリの Settings
- 左メニューの Branches
- Branch protection rules セクション
- Add rule または既存ルールを編集
mainへの直接プッシュ禁止
Section titled “mainへの直接プッシュ禁止”- Branch name pattern に
mainを入力 - Require a pull request before merging にチェック
これにより:✅ PRを経由したマージのみ許可❌ git push origin main は拒否される# 直接プッシュしようとすると...git push origin main
# エラーになるremote: error: GH006: Protected branch update failedremote: error: Required pull request reviews not satisfiedPR必須化の設定
Section titled “PR必須化の設定”Require a pull request before merging
Section titled “Require a pull request before merging”| サブオプション | 説明 |
|---|---|
| Require approvals | 承認が必要 |
| Dismiss stale pull request approvals | 新コミットで承認を無効化 |
| Require review from Code Owners | CODEOWNERSの承認必須 |
| Require approval of the most recent push | 最新プッシュの承認必須 |
必須承認数の設定
Section titled “必須承認数の設定”「Required number of approvals before merging」で1〜6を選択:
| チーム規模 | 推奨承認数 |
|---|---|
| 1〜3人 | 1 |
| 4〜10人 | 1〜2 |
| 10人以上 | 2以上 |
承認の取り消し(Dismiss stale approvals)
Section titled “承認の取り消し(Dismiss stale approvals)”新しいコミットがプッシュされたら、既存の承認を無効にする設定:
メリット:- 承認後の変更も必ずレビューされる- 意図しない変更の混入を防止
デメリット:- 軽微な修正でも再承認が必要- レビュアーの負担増ステータスチェック必須化
Section titled “ステータスチェック必須化”Require status checks to pass before merging
Section titled “Require status checks to pass before merging”CI/CDの結果をマージ条件にします。
name: CI
on: pull_request: branches: [main]
jobs: test: # ← このジョブ名を必須チェックに設定 runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Run tests run: npm test
lint: # ← このジョブ名も必須チェックに追加可能 runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Lint run: npm run lint- Require status checks to pass before merging にチェック
- 検索ボックスでジョブ名を検索(例:
test,lint) - 必須にしたいチェックを選択
Require branches to be up to date before merging
Section titled “Require branches to be up to date before merging”マージ前にbaseブランチ(main)と同期することを必須にします:
有効にすると:- PRブランチがmainの最新に追随している必要がある- 「Update branch」ボタンでワンクリック同期管理者への適用
Section titled “管理者への適用”Do not allow bypassing the above settings
Section titled “Do not allow bypassing the above settings”デフォルトでは、リポジトリ管理者(Admin)は保護ルールをバイパスできます。
このオプションを有効にすると、管理者も同じルールに従う必要があります。
推奨:- 本番環境に影響するリポジトリ → 有効- 個人プロジェクト → 無効でも可署名付きコミット必須化
Section titled “署名付きコミット必須化”Require signed commits
Section titled “Require signed commits”すべてのコミットにGPG署名を必須にします。
# 署名付きコミットの確認git log --show-signature
# 署名付きでコミットgit commit -S -m "メッセージ"
# 常に署名する設定git config --global commit.gpgsign true設定する理由
Section titled “設定する理由”- なりすまし防止: コミッターの本人確認
- 監査対応: コードの出所を証明
- セキュリティ: サプライチェーン攻撃対策
線形履歴の強制
Section titled “線形履歴の強制”Require linear history
Section titled “Require linear history”マージコミットを禁止し、履歴を直線的に保ちます。
許可されるマージ方法:✅ Squash and merge✅ Rebase and merge❌ Create a merge commit非線形履歴:●────●────● │ ↑ └──●─┘
線形履歴:●────●────●────●────●Force Push の制限
Section titled “Force Push の制限”Allow force pushes
Section titled “Allow force pushes”| 設定 | 説明 |
|---|---|
| Disabled | 完全に禁止(推奨) |
| Everyone | 全員に許可(非推奨) |
| Specify who can force push | 特定ユーザーのみ許可 |
ブランチ削除の制限
Section titled “ブランチ削除の制限”Allow deletions をオフにすると、保護ブランチの削除を禁止できます。
ルールの優先順位
Section titled “ルールの優先順位”複数のルールがある場合、最も制限的なルールが適用されます:
例:ルール1: main → 承認1件必須ルール2: main* → 承認2件必須
main ブランチには両方がマッチ → 2件必須(より制限的)中級者向けTips
Section titled “中級者向けTips”ワイルドカードパターン
Section titled “ワイルドカードパターン”main # 完全一致release/* # release/v1.0.0 などにマッチfeature/** # feature/auth/login などにもマッチ* # 全ブランチ設定のエクスポート/インポート
Section titled “設定のエクスポート/インポート”GitHub CLI でルールを確認:
gh api repos/{owner}/{repo}/branches/main/protection一時的なルール緩和
Section titled “一時的なルール緩和”緊急時に管理者がバイパスする場合:
- 「Do not allow bypassing」を一時的にオフ
- 緊急修正を実施
- 設定を元に戻す
- 監査ログに記録
Q1. mainブランチへの直接プッシュを禁止するために有効にする設定はどれですか?
Q2. Dismiss stale pull request approvalsの効果はどれですか?
Q3. ブランチ保護ルールで複数のルールがマッチする場合、どのルールが適用されますか?