如何通过编程操作Azure DevOps仓库:创建分支、修改并推送变更?
自动化Azure DevOps仓库操作的工具库推荐
C#
使用Azure DevOps .NET SDK(官方提供)可直接对接Azure DevOps REST API,完成分支创建、内容修改与推送操作:
- 创建分支:通过
GitHttpClient的CreateRefAsync方法,基于现有分支生成新分支 - 修改并推送内容:借助
GitHttpClient的CreatePushAsync方法,结合GitCommitRef和GitChange对象提交文件变更
示例代码片段:
// 初始化客户端 var connection = new VssConnection(new Uri("https://dev.azure.com/your-org"), new VssBasicCredential("", "personal-access-token")); var gitClient = connection.GetClient<GitHttpClient>(); // 创建新分支 var newRef = new GitRefUpdate() { Name = "refs/heads/new-branch", OldObjectId = "existing-branch-sha", NewObjectId = "existing-branch-sha" // 基于现有分支创建 }; await gitClient.UpdateRefsAsync(new[] { newRef }, "your-project", "your-repo"); // 提交并推送文件变更 var commit = new GitCommitRef() { Comment = "Automated change", Changes = new List<GitChange>() { new GitChange() { ChangeType = VersionControlChangeType.Edit, Item = new GitItem() { Path = "/path/to/file.txt" }, NewContent = new ItemContent() { Content = "Updated content", ContentType = ItemContentType.RawText } } } }; await gitClient.CreatePushAsync(new GitPush() { Commits = new[] { commit }, RefUpdates = new[] { newRef } }, "your-project", "your-repo");
Go
推荐使用Azure DevOps Go SDK(官方维护),也可结合go-git库操作Git内容后对接Azure DevOps API:
- 创建分支:调用SDK的Git Refs接口生成新分支
- 修改内容:用
go-git克隆仓库(或直接操作远程内容),修改文件后提交推送
示例代码片段(使用官方SDK创建分支):
package main import ( "context" "github.com/microsoft/azure-devops-go-api/azuredevops/git" ) func main() { ctx := context.Background() connection, _ := git.NewClient(ctx, "https://dev.azure.com/your-org", "personal-access-token") // 创建新分支参数 refUpdate := git.GitRefUpdate{ Name: pointer.String("refs/heads/new-branch"), OldObjectId: pointer.String("existing-branch-sha"), NewObjectId: pointer.String("existing-branch-sha"), } _, err := connection.UpdateRefs(ctx, git.UpdateRefsArgs{ Project: "your-project", RepositoryId: pointer.String("your-repo-id"), Refs: []git.GitRefUpdate{refUpdate}, }) if err != nil { panic(err) } }
若需修改文件内容,搭配go-git克隆仓库、修改后推送即可,注意用Azure DevOps个人访问令牌作为Git密码。
Python
使用官方azure-devops Python包,直接调用API完成全流程操作:
- 安装:
pip install azure-devops - 创建分支:通过
git_client.update_refs方法生成新分支 - 修改并推送:通过
git_client.create_push提交文件变更
示例代码片段:
from azure.devops.connection import Connection from azure.devops.v7_1.git.models import GitRefUpdate, GitCommitRef, GitChange, VersionControlChangeType, ItemContent, ItemContentType from msrest.authentication import BasicAuthentication # 初始化连接 personal_access_token = 'your-pat' organization_url = 'https://dev.azure.com/your-org' credentials = BasicAuthentication('', personal_access_token) connection = Connection(base_url=organization_url, creds=credentials) git_client = connection.clients.get_git_client() # 创建新分支 new_ref = GitRefUpdate( name='refs/heads/new-branch', old_object_id='existing-branch-sha', new_object_id='existing-branch-sha' ) git_client.update_refs([new_ref], project='your-project', repository_id='your-repo-id') # 提交文件变更 commit = GitCommitRef( comment='Automated change', changes=[ GitChange( change_type=VersionControlChangeType.EDIT, item={'path': '/path/to/file.txt'}, new_content=ItemContent( content='Updated content', content_type=ItemContentType.RAW_TEXT ) ) ] ) git_client.create_push( push={'commits': [commit], 'ref_updates': [new_ref]}, project='your-project', repository_id='your-repo-id' )
注意:所有操作需提前获取Azure DevOps个人访问令牌(PAT),并赋予对应Git仓库权限(如分支创建、代码推送等)。
内容的提问来源于stack exchange,提问作者Paras
相关产品推荐
相关产品推荐

