Forum Discussion

LanceLyons's avatar
LanceLyons
Copper Contributor
Jan 08, 2024

Auto create 3 branches when a repository is created in Azure DevOps Git

Hi,

Is there a way to trigger the creation of branches when a new repository is created in Azure Git?

I know we can automate the creation of an azure git repository  and then create the branches via powershell.  However, we have developers that will eventually create their own repositories and it would be nice if we could automatically see the new repository and then create a development, release and hotfix branch.


2 Replies

  • LanceLyons's avatar
    LanceLyons
    Copper Contributor

    Thanks. I looked at that approach and did actually implement it but it seemed like branches I had in my template repo, did not actually get created in the new repo unless I first created them in the template repo during the process. so my process in a pipeline looks like this now but it works. not sure I needed the template repo for this however


     

    az repos create --name a.test --organization https://dev.azure.com/org/ --project Onlife
    
    git clone -q https://$(PAT)@dev.azure.com/org/project/_git/repo.template
    cd repo.template
    git config --global user.email "email address removed for privacy reasons"
    
    git config --global user.name "lance_lyons"
    --------------   added to have breanches show up in new repo -----------------
    git checkout -b release
    git checkout -b development
    git checkout -b hotfix
    git add -A
    --------------------------------------------------------
    git commit -am "Copied from project repo.template"
    git remote rename origin repo.template
    git remote add origin https://$(PAT)@dev.azure.com/org/project/_git/a.test
    git push -u origin main
    git push -u origin --all

     

     

  • JohnLokerse's avatar
    JohnLokerse
    Copper Contributor

    LanceLyons 

     

    Hi Lance,

     

    There's no built-in feature to automatically create Azure DevOps repositories with 3 default branches. However, you can implement ways to achieve what you want such as an automated process to create Git repos with default branches as you've mentioned. 

    I would take it a step further by creating a "Template Repository". This template repository is a repository in Azure DevOps and has the three git branches (dev, release and hotfix). If you want you can add a specific folder structure or have an initial project setup so the developers can start right away. Your Azure Pipeline would roughly look something like this:

    1. Checkout template repository

    2. Create the requested Azure DevOps repository

    3. Do a checkout of the created repository

    4. Set the remote origin to the remote Azure DevOps repository url 

    5. Run the pipeline through the Azure DevOps GUI, via the REST API or other tooling 😉

     

    Let me know if this helps you and if you have any questions, let me know!

Resources