powershell 删除分支并从现有分支创建新分支

omvjsjqw  于 2022-11-10  发布在  Shell
关注(0)|答案(1)|浏览(136)

我希望删除SBX分支,并利用现有的DEV分支创建一个名为SBX的新分支,因此本质上是用DEV覆盖SBX。如果有一种方法可以一下子用一个分支重写另一个分支,我也会感兴趣的。参考

的屏幕截图
我想用PowerShell/API来做这件事。我看了下面的URL Refs - List,但不知道如何使用这些API。
例如,我运行以下命令来获取我的分支机构的列表

https://dev.azure.com/jaredsplayground/JaredsPlayground/_apis/git/repositories/Jared/refs?api-version=6.0

但要拿回以下几点:
Azure DevOps服务|登录
有没有想过我做错了什么以及如何用DEV覆盖SBX?
谢谢,

odopli94

odopli941#

未尝试覆盖。但我们可以在DEV分支的基础上创建新的分支。
请确保您已创建PAT并选择了正确的作用域。详情见Use personal access tokens
通常,我们可以根据已有的分支或提交创建分支,因此newObjectId实际上是特定分支或提交ID的ObjectID
我们可以通过以下REST API获取现有分支的ObjectID

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repository_ID}/refs?api-version=5.1

并使用此REST API获得提交:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repository_ID}/commits?api-version=5.1

然后,我们可以使用特定的ObjectIDCommitID创建新分支:
下面是PowerShell脚本,供您参考以创建新分支机构:

Param(
   [string]$collectionurl = "https://dev.azure.com/{organization}",
   [string]$project = "projectname",
   [string]$repoid = "62c8ce54-a7bb-4e08-8ed7-40b27831bd8b",
   [string]$Newbranch_name = "Test",
   [string]$newObjectId= "Existing Branch objectID or the commit ID ",
   [string]$user = "",
   [string]$token = "PAT-Here"
)

# Base64-encodes the Personal Access Token (PAT) appropriately

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
function CreateJsonBody
{
    $value = @"
    [ { "name": "refs/heads/$Newbranch_name",
        "oldObjectId": "0000000000000000000000000000000000000000", 
        "newObjectId": "$newObjectId" } ]
"@
 return $value
}
$json = CreateJsonBody

# Create new Branch

$NewBranch = "$collectionurl/$project/_apis/git/repositories/$repoid/refs?api-version=5.1" 
Invoke-RestMethod -Uri $NewBranch -Method POST -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

更新:

DevOps在TFVC回购上没有同等的REST API。请参见TFVC。您可以尝试使用相关的TFVC commandstf branch作为参考。
您还可以尝试使用Git回购而不是TFVC回购。只需将TFVC回购导入到新的Git回购。详细信息请参见Import repositories from TFVC to Git

相关问题