Created
June 15, 2020 16:58
-
-
Save SQL-MisterMagoo/1c3923b7b92dae39b094b6f008d706ec to your computer and use it in GitHub Desktop.
Change default branch on all your Github repos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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