Skip to content

Instantly share code, notes, and snippets.

@SQL-MisterMagoo
Created June 15, 2020 16:58
Show Gist options
  • Save SQL-MisterMagoo/1c3923b7b92dae39b094b6f008d706ec to your computer and use it in GitHub Desktop.
Save SQL-MisterMagoo/1c3923b7b92dae39b094b6f008d706ec to your computer and use it in GitHub Desktop.
Change default branch on all your Github repos
$token = <your github personal token>
$headers = @{"Authorization"="token $token"}
$body = (ConvertTo-Json @{ default_branch='main' })
Write-Host "Fetching repo list"
[Console]::Out.Flush()
$repos = Invoke-RestMethod -Headers $headers -Method GET -Uri "https://api.github.com/user/repos?type=owner&per_page=80"
foreach($repo in $repos) {
if($repo.default_branch -eq "master")
{
$branch = $null
$branch = Invoke-RestMethod -Headers $headers -Method GET -Uri "$($repo.url)/branches/main"
if ($branch -ne $null) {
Write-Host "Updating default branch to main for $($repo.name)"
[Console]::Out.Flush()
$result = Invoke-RestMethod -Headers $headers -Method PATCH -Uri $repo.url -Body $body
Write-Host "New default branch is $($result.default_branch)"
[Console]::Out.Flush()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment