Set-GitHubRepositoryBranchProtection

SYNOPSIS

Update branch protection

SYNTAX

Default (Default)

Set-GitHubRepositoryBranchProtection [-Owner] <string> [-Repo] <string> [-Branch] <string>
 -RequiredStatusChecks <Object> -EnforceAdmins -RequiredPullRequestReviews <Object>
 -Restrictions <Object> [-RequiredLinearHistory] [-AllowForcePushes] [-AllowDeletions]
 [-BlockCreations] [-RequiredConversationResolution] [-LockBranch] [-AllowForkSyncing]
 [-Accept <string>] [-WhatIf] [-Confirm]

Body

Set-GitHubRepositoryBranchProtection [-Owner] <string> [-Repo] <string> [-Branch] <string>
 -Body <Object> [-RequiredStatusChecks <Object>] [-EnforceAdmins]
 [-RequiredPullRequestReviews <Object>] [-Restrictions <Object>] [-RequiredLinearHistory]
 [-AllowForcePushes] [-AllowDeletions] [-BlockCreations] [-RequiredConversationResolution]
 [-LockBranch] [-AllowForkSyncing] [-Accept <string>] [-WhatIf] [-Confirm]

ALIASES

This cmdlet has the following aliases,

  • Get-GHSetRepositoryBranchProtection

DESCRIPTION

The Set-GitHubRepositoryBranchProtection cmdlet invokes the PUT method of the /repos/{owner}/{repo}/branches/{branch}/protection endpoint (Update branch protection)

Protected branches are available in public repositories with GitHub Free and GitHub Free for organizations, and in public and private repositories with GitHub Pro, GitHub Team, GitHub Enterprise Cloud, and GitHub Enterprise Server. For more information, see GitHub’s products (https://docs.github.com/github/getting-started-with-github/githubs-products) in the GitHub Help documentation.

Protecting a branch requires admin or owner permissions to the repository. [!NOTE] Passing new arrays of ‘users’ and ‘teams’ replaces their previous values. [!NOTE] The list of users, apps, and teams in total is limited to 100 items.

Endpoint: PUT /repos/{owner}/{repo}/branches/{branch}/protection API version: 2026-03-10

EXAMPLES

EXAMPLE 1

' -RequiredStatusChecks @{ key = 'value' } -EnforceAdmins -RequiredPullRequestReviews @{ key = 'value' } -Restrictions @{ key = 'value' } -Body @{ key = 'value' }

Invokes PUT /repos/{owner}/{repo}/branches/{branch}/protection

EXAMPLE 2

' -Body @{ required_status_checks = @{ key = 'value' } }

Sends the request body from a hashtable, parameters override the keys of -Body

PARAMETERS

-Accept

The Accept header (media type) of the request Default value: application/vnd.github+json

Type: System.String
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-AllowDeletions

Allows deletion of the protected branch by anyone with write access to the repository. Set to ‘false’ to prevent deletion of the protected branch. Default: ‘false’. For more information, see “Enabling force pushes to a protected branch (https://docs.github.com/github/administering-a-repository/enabling-force-pushes-to-a-protected-branch)” in the GitHub Help documentation.

Body property [ allow_deletions ]

Type: System.Management.Automation.SwitchParameter
DefaultValue: False
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-AllowForcePushes

Permits force pushes to the protected branch by anyone with write access to the repository. Set to ‘true’ to allow force pushes. Set to ‘false’ or ‘null’ to block force pushes. Default: ‘false’. For more information, see “Enabling force pushes to a protected branch (https://docs.github.com/github/administering-a-repository/enabling-force-pushes-to-a-protected-branch)” in the GitHub Help documentation.”

Body property [ allow_force_pushes ]

Type: System.Management.Automation.SwitchParameter
DefaultValue: False
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-AllowForkSyncing

Whether users can pull changes from upstream when the branch is locked. Set to ‘true’ to allow fork syncing. Set to ‘false’ to prevent fork syncing. Default: ‘false’.

Body property [ allow_fork_syncing ]

Type: System.Management.Automation.SwitchParameter
DefaultValue: False
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-BlockCreations

If set to ‘true’, the ‘restrictions’ branch protection settings which limits who can push will also block pushes which create new branches, unless the push is initiated by a user, team, or app which has the ability to push. Set to ‘true’ to restrict new branch creation. Default: ‘false’.

Body property [ block_creations ]

Type: System.Management.Automation.SwitchParameter
DefaultValue: False
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Body

The whole request body as a hashtable or object. Keys that are also available as parameters are overwritten by the parameter values

Type: System.Object
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: Body
  Position: Named
  IsRequired: true
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Branch

The name of the branch. Cannot contain wildcard characters. To use wildcard characters in branch names, use the GraphQL API (https://docs.github.com/graphql).

Path parameter [ branch ]

Type: System.String
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 2
  IsRequired: true
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: true
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Confirm

Prompts you for confirmation before running the cmdlet.

Type: System.Management.Automation.SwitchParameter
DefaultValue: ''
SupportsWildcards: false
Aliases:
- cf
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-EnforceAdmins

Enforce all configured restrictions for administrators. Set to ‘true’ to enforce required status checks for repository administrators. Set to ‘null’ to disable.

Required unless -Body is used

Body property [ enforce_admins ]

Type: System.Management.Automation.SwitchParameter
DefaultValue: False
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: Body
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
- Name: Default
  Position: Named
  IsRequired: true
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-LockBranch

Whether to set the branch as read-only. If this is true, users will not be able to push to the branch. Default: ‘false’.

Body property [ lock_branch ]

Type: System.Management.Automation.SwitchParameter
DefaultValue: False
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Owner

The account owner of the repository. The name is not case sensitive.

Path parameter [ owner ]

Type: System.String
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 0
  IsRequired: true
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: true
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Repo

The name of the repository without the ‘.git’ extension. The name is not case sensitive.

Path parameter [ repo ]

Type: System.String
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 1
  IsRequired: true
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: true
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-RequiredConversationResolution

Requires all conversations on code to be resolved before a pull request can be merged into a branch that matches this rule. Set to ‘false’ to disable. Default: ‘false’.

Body property [ required_conversation_resolution ]

Type: System.Management.Automation.SwitchParameter
DefaultValue: False
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-RequiredLinearHistory

Enforces a linear commit Git history, which prevents anyone from pushing merge commits to a branch. Set to ‘true’ to enforce a linear commit history. Set to ‘false’ to disable a linear commit Git history. Your repository must allow squash merging or rebase merging before you can enable a linear commit history. Default: ‘false’. For more information, see “Requiring a linear commit history (https://docs.github.com/github/administering-a-repository/requiring-a-linear-commit-history)” in the GitHub Help documentation.

Body property [ required_linear_history ]

Type: System.Management.Automation.SwitchParameter
DefaultValue: False
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-RequiredPullRequestReviews

Require at least one approving review on a pull request, before merging. Set to ‘null’ to disable.

Required unless -Body is used

Body property [ required_pull_request_reviews ]

Type: System.Object
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: Body
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
- Name: Default
  Position: Named
  IsRequired: true
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-RequiredStatusChecks

Require status checks to pass before merging. Set to ‘null’ to disable.

Required unless -Body is used

Body property [ required_status_checks ]

Type: System.Object
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: Body
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
- Name: Default
  Position: Named
  IsRequired: true
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Restrictions

Restrict who can push to the protected branch. User, app, and team ‘restrictions’ are only available for organization-owned repositories. Set to ‘null’ to disable.

Required unless -Body is used

Type: System.Object
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: Body
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
- Name: Default
  Position: Named
  IsRequired: true
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-WhatIf

Runs the command in a mode that only reports what would happen without performing the actions.

Type: System.Management.Automation.SwitchParameter
DefaultValue: ''
SupportsWildcards: false
Aliases:
- wi
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

Objects with properties named like the path parameters can be piped to this function

{{ Fill in the Description }}

System.String

{{ Fill in the Description }}

OUTPUTS

The response of the GitHub API (objects

{{ Fill in the Description }}

NOTES

OperationId: repos/update-branch-protection Category: branches / branch-protection