Set Default Branch in Azure Repos

Max Yermakhanov
ObjectSharp (a Centrilogic Company)
2 min readMar 10, 2020

Quite often master branch is not your default branch for your team’s pull requests. No matter how much you want it to be. Often, teams are using develop branch as a sort of a buffer/integration branch to give them extra flexibility before committing to master. If you want to change a default branch for your repo, you can follow the following steps one repo at a time:

  1. Navigate to your project and select Project settings.
  2. Scroll down and select Repositories from the Code section.
  3. Select the desired repository and expand the branches.
  4. Select the … beside the desired branch and choose Set as default branch.

Or, you can use az devops cli commands to change default branch for all of your repos:

TOKEN=ENTER_YOUR_TOKEN_HEREORGANIZATION=ENTER_YOUR_ORGANIZATION_HEREPROJECT=ENTER_YOUR_PROJECT_HEREDEFAULT_BRANCH=ENTER_YOUR_NEW_DEFAULT_BRANCH_HERE
#define REST API URL to get all Azure Repos
repos_URL="https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories?api-version=4.1"

#get all Azure Repos IDs
repo_ids=$(curl -s -X GET -u PAT:$TOKEN $repos_URL | jq -r '.value[].id')

#authenticate to Azure DevOps CLI
$env:AZURE_DEVOPS_EXT_PAT=$TOKEN
az devops login --organization https://dev.azure.com/$ORGANIZATION

#walk through all repos in the team project and assign new default branch
for repo_id in $repo_ids; do
echo "setting default branch for $repo_id repo to $DEFAULT_BRANCH"az repos update --org "$ORG_URL" --project "$PROJECT" --repository $repo_id --default-branch $DEFAULT_BRANCHdone

Hope this helps.

#DevOpsGuy

--

--