You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell/API将SBX分支覆盖为DEV分支及API登录问题排查

解决Azure DevOps分支覆盖及API授权问题

一、API调用返回登录提示的原因及修复

直接调用Azure DevOps API时出现登录提示,是因为请求未携带有效授权凭据。Azure DevOps的REST API需要身份验证,最常用的方式是使用个人访问令牌(PAT)。

在PowerShell中调用API时,需将PAT转换为Base64编码的凭据并添加到请求头,示例代码如下:

$pat = "你的个人访问令牌"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat"))

# 调用分支列表API的示例
$url = "https://dev.azure.com/jaredsplayground/JaredsPlayground/_apis/git/repositories/Jared/refs?api-version=6.0"
Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $base64AuthInfo"}

注意:创建PAT时需分配代码读写权限,确保权限范围足够操作分支。

二、用DEV分支覆盖SBX分支的实现方法

方法1:分步操作(删除旧SBX再重新创建)

适合需要彻底清理旧分支历史的场景,分两步执行:

  1. 删除现有SBX分支
    使用Azure DevOps的Refs - Delete API:
    $deleteUrl = "https://dev.azure.com/jaredsplayground/JaredsPlayground/_apis/git/repositories/Jared/refs?api-version=6.0"
    $deleteBody = @{
        name = "refs/heads/SBX"
    } | ConvertTo-Json
    
    Invoke-RestMethod -Uri $deleteUrl -Method Delete -Headers @{Authorization = "Basic $base64AuthInfo"} -Body $deleteBody -ContentType "application/json"
    
  2. 基于DEV分支创建新的SBX分支
    先获取DEV分支的最新提交哈希,再调用Refs - Create API创建分支:
    # 获取DEV分支最新提交哈希
    $devRefUrl = "https://dev.azure.com/jaredsplayground/JaredsPlayground/_apis/git/repositories/Jared/refs?filter=heads/DEV&api-version=6.0"
    $devRef = Invoke-RestMethod -Uri $devRefUrl -Headers @{Authorization = "Basic $base64AuthInfo"}
    $devCommitId = $devRef.value[0].objectId
    
    # 创建新SBX分支
    $createUrl = "https://dev.azure.com/jaredsplayground/JaredsPlayground/_apis/git/repositories/Jared/refs?api-version=6.0"
    $createBody = @{
        name = "refs/heads/SBX"
        oldObjectId = "0000000000000000000000000000000000000000" # 空值表示创建新分支
        newObjectId = $devCommitId
    } | ConvertTo-Json
    
    Invoke-RestMethod -Uri $createUrl -Method Post -Headers @{Authorization = "Basic $base64AuthInfo"} -Body $createBody -ContentType "application/json"
    

方法2:一步到位强制覆盖(无需删除分支)

直接通过强制推送的方式,用DEV的提交覆盖SBX分支,适合保留分支存在性的场景:

方式A:PowerShell结合Git命令(需本地安装Git)

# 克隆仓库(本地无仓库时执行)
git clone https://dev.azure.com/jaredsplayground/JaredsPlayground/_git/Jared
cd Jared

# 拉取DEV分支最新代码
git checkout DEV
git pull origin DEV

# 强制将DEV推送到SBX分支
git push origin DEV:SBX -f

方式B:使用Azure DevOps Pushes - Create API

# 获取DEV分支最新提交哈希
$devRefUrl = "https://dev.azure.com/jaredsplayground/JaredsPlayground/_apis/git/repositories/Jared/refs?filter=heads/DEV&api-version=6.0"
$devRef = Invoke-RestMethod -Uri $devRefUrl -Headers @{Authorization = "Basic $base64AuthInfo"}
$devCommitId = $devRef.value[0].objectId

# 获取SBX分支当前提交哈希
$sbxRefUrl = "https://dev.azure.com/jaredsplayground/JaredsPlayground/_apis/git/repositories/Jared/refs?filter=heads/SBX&api-version=6.0"
$sbxRef = Invoke-RestMethod -Uri $sbxRefUrl -Headers @{Authorization = "Basic $base64AuthInfo"}
$sbxOldCommitId = $sbxRef.value[0].objectId

# 发起强制推送请求
$pushUrl = "https://dev.azure.com/jaredsplayground/JaredsPlayground/_apis/git/repositories/Jared/pushes?api-version=6.0"
$pushBody = @{
   refUpdates = @(
       @{
           name = "refs/heads/SBX"
           oldObjectId = $sbxOldCommitId
           newObjectId = $devCommitId
       }
   )
   commits = @()
} | ConvertTo-Json

Invoke-RestMethod -Uri $pushUrl -Method Post -Headers @{Authorization = "Basic $base64AuthInfo"} -Body $pushBody -ContentType "application/json"

注意:强制推送会覆盖SBX分支的所有历史记录,操作前需确认无未保存的重要更改。


内容的提问来源于stack exchange,提问作者jssteele89

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 17:25:26