LoginSignup
6

More than 1 year has passed since last update.

【Git】ブランチを保護する方法【GitHub】

Last updated at Posted at 2022-08-14

設定方法

対象のリモートリポジトリにアクセスし、SettingsBranchesを選択します。
スクリーンショット 2022-08-14 午後5.22.42.png

Add ruleをクリックします。
スクリーンショット 2022-08-14 午後5.25.54.png

Branch name patternに対象ブランチの名前を入力しProtect matching branchesおよびRules applied to everyone including administratorsから適用するルールを選択します。
選択後Createをクリックします。
スクリーンショット 2022-08-14 午後5.29.40.png

設定例

以下のように設定することでmainブランチへの直pushを禁止できます。
スクリーンショット 2022-08-26 14.43.47.png

mainブランチに直接pushしようとすると以下のようなエラーが出ます。

ターミナル
$ git push origin main
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 8 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 659 bytes | 659.00 KiB/s, done.
Total 5 (delta 4), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (4/4), completed with 4 local objects.
remote: error: GH006: Protected branch update failed for refs/heads/main.
remote: error: At least 1 approving review is required by reviewers with write access.
To github.com:GitHubUser/sample-app.git
 ! [remote rejected] main -> main (protected branch hook declined)
error: failed to push some refs to 'github.com:GitHubUser/sample-app.git'

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
6