Set the default branch on your Azure DevOps Repos

--

Introduction

« Breaking News » the Git project has decided to use as default branch main instead of master.

Git default branch

As very well described in the following article, Microsoft has set on november 2020 the default branch of all new Azure DevOps Repos to be main instead of master.

“Microsoft is joining the Git project (statement, code change) and multiple vendors including GitHub in delivering these changes. As an industry, we’re making an effort to move towards more inclusive language.”

New Git Repos

New Repos will inherit from a policy that set the default branch name for new Repos.

This policy is set at the different levels:

  1. The Organization level under “Organization Settings” -> “Repositories”
  2. or at the Project level under “Project Settings” -> “Repositories” -> “Settings”
New Git Repos

Existing Repos

I will share here few commands to set your Azure DevOps default branch. There is a popular way to do that through Git cmdlet by renaming the master branch, I will share here another approach which consist in creating a new branch and locking the old one through Azure DevOps CLI extension.

In order to proceed you will need the following prerequisites :

Set up variables

Since these variables are re-used, a locals block makes this more maintainable.

organization=”https://dev.azure.com/organization_name/"targetBranch=”main”

Connection

Connect directly through Azure AD or use a Personal Access Token that has the minimum privileges :

  • Read privilege a the Projet and Team level.
  • Full privilege at the Code level.
az login# ORaz devops login

Script & Workflow

  1. List all our Azure DevOps projects
  2. List our Azure DevOps repositories
  3. List our ref to determine the current default branch
  4. Create the new ref if it doesn’t exist
  5. Update the repo with the new default branch
  6. Lock the old default branch
New Default Branch

The script →

Conclusion

Changing a default Git branch might not be as simple as mentioned in the upper chapter, some additional steps might be needed in specific context like when the default branch is used to publish application or when application are directly plugged in your default branch.

Those are some recommendations that I used to share :

  1. Use the same default branch name for all your repos.
  2. Fit with global rules. I would say that when the Git project define a default branch name, it’s a global rule. So I will use main as default branch.
  3. Use a custom branch to publish into production.

I hope you enjoyed the article.

See You in the Cloud.

Jamesdld

👋 Join FAUN today and receive similar stories each week in your inbox! Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--