简体   繁体   English

Azure DevOps 存储库中的多个初始分支

[英]Multiple Initial Branches in Azure DevOps Repositories

I was wondering if it is possible to make Azure DevOps to automatically create both master and develop branches upon creating a new repository.我想知道是否有可能使 Azure DevOps 在创建新存储库时自动创建master分支和develop分支。 I want both branches protected.我希望两个分支都受到保护。

If project level branch policies are set up for both, but only master is created by default, when creating develop manually via git push --set-upstream origin develop I am able to push a number of commits directly on the branch, the protective policies are only applied after.如果为两者都设置了项目级分支策略,但默认情况下只创建master ,当通过git push --set-upstream origin develop develop创建开发时,我可以直接在分支上推送一些提交,保护策略仅在之后应用。

You could use REST API to automatically create a new repo with two branch created at the same time.您可以使用 REST API 自动创建一个新的 repo,同时创建两个分支。 Use API to Create a repository first, and get repository Id from the response.使用 API 首先创建一个存储库,并从响应中获取存储库 ID。 Use the $RepoID in next API to Create new branch .使用下一个 API 中的 $RepoID 创建新分支

You could create two new branches with the oldObjectId "0000000000000000000000000000000000000000".您可以使用 oldObjectId“0000000000000000000000000000000000000000”创建两个新分支。 Here is the PowerShell script that works for me.这是对我有用的 PowerShell 脚本。 Need to replace orgName, PAT, NewRepoName, ProjectID, branch name .需要替换orgName, PAT, NewRepoName, ProjectID, branch name

$url = "https://dev.azure.com/<orgName>/_apis/git/repositories?api-version=7.0"
$AzureDevOpsPAT = '<PAT>'
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AzureDevOpsPAT)")) }
$body = "{`"name`": `"<NewRepoName>`", `"project`": {`"id`": `"<ProjectID>`"}}"
$response = Invoke-RestMethod -Uri $url -Method POST -Headers $AzureDevOpsAuthenicationHeader -ContentType "application/json" -Body $body
$response | ConvertTo-Json
$RepoID = $response.id

$url2 = "https://dev.azure.com/<orgName>/_apis/git/repositories/$RepoID/pushes?api-version=5.1"
$body2 = "{`"refUpdates`": [{`"name`": `"refs/heads/<master>`", `"oldObjectId`": `"0000000000000000000000000000000000000000`"}], `"commits`": [{`"comment`": `"Initial commit.`", `"changes`": [{`"changeType`": `"add`", `"item`": {`"path`": `"/readme.md`"}, `"newContent`": {`"content`": `"My first file!`", `"contentType`": `"rawtext`"}}]}]}"
Invoke-RestMethod -Uri $url2 -Method POST -Headers $AzureDevOpsAuthenicationHeader -ContentType "application/json" -Body $body2

$body3 = "{`"refUpdates`": [{`"name`": `"refs/heads/<develop>`", `"oldObjectId`": `"0000000000000000000000000000000000000000`"}], `"commits`": [{`"comment`": `"Initial commit.`", `"changes`": [{`"changeType`": `"add`", `"item`": {`"path`": `"/readm.md`"}, `"newContent`": {`"content`": `"My first file!`", `"contentType`": `"rawtext`"}}]}]}"
Invoke-RestMethod -Uri $url2 -Method POST -Headers $AzureDevOpsAuthenicationHeader -ContentType "application/json" -Body $body3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure Devops - 多个存储库 - Azure Devops - Multiple repositories Azure DevOps - 收集在 PR 上“需要审阅者”以及哪些分支的存储库列表 - Azure DevOps - Gather a list of repositories that "Require Reviewers" on PRs and for what branches 具有多个存储库的 Azure DevOps 容器作业 - Azure DevOps Container Job with Multiple Repositories Azure DevOps Git - 归档多个存储库 - Azure DevOps Git - Archive multiple repositories Azure DevOps:一个存储库中有多个存储库或多个文件夹? - Azure DevOps: Multiple repositories or multiple folders in one repository? Azure Devops 回购分支 - Azure Devops Repo branches 在 2 个不同的存储库中管理 Azure DEVOPS Git DEV 和发布分支是个好主意吗? - Is it good idea to Manage Azure DEVOPS Git DEV and Release Branches in 2 Different Repositories? 如何在 Azure DevOps 中查看用户跨多个存储库的提交? - How to see commits by user accross multiple repositories in Azure DevOps? Azure Devops Release Pipeline 中针对不同分支的多个调度 - Multiple schedules in Azure Devops Release Pipeline for different branches Azure DevOps - 如何轻松切换分支以用于多个环境 - Azure DevOps - How to easily switch branches to use for multiple environments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM