Skip to content

Instantly share code, notes, and snippets.

@maboloshi
Created December 8, 2023 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maboloshi/59ccad329b0fc2ad17b8cf462b9c82e6 to your computer and use it in GitHub Desktop.
Save maboloshi/59ccad329b0fc2ad17b8cf462b9c82e6 to your computer and use it in GitHub Desktop.
通过 GitHub REST API 创建远程分支 (Powershell 原生实现)
function create_ref {
param (
[Parameter(Mandatory = $true)]
[String]$Token,
[Parameter(Mandatory = $true)]
[String]$RepoNwo,
[Parameter(Mandatory = $true)]
[String]$Ref,
[Parameter(Mandatory = $true)]
[String]$CommitSha
)
$github_api_url = "https://api.github.com/repos/$RepoNwo/git/refs"
$headers = @{
"Authorization" = "token $Token"
}
$body = @{
ref = $Ref
sha = $CommitSha
} | ConvertTo-Json
Write-Host "Invoke-RestMethod -Method Post -Uri $github_api_url -ContentType 'application/json' -Headers $headers -Body $body"
Invoke-RestMethod -Method Post -Uri $github_api_url -ContentType 'application/json' -Headers $headers -Body $body
}
# Create remote branch vis GitHub REST API
$ParentSHA = $((git ls-remote --refs --quiet origin $OriginBranch).Split()[0])
$response = create_ref -t $Token -RepoNwo $REPOSITORY -Ref "refs/heads/$branch" -CommitSha $ParentSHA
Write-Host $response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment