Azure DevOps - Cloning and updating all Git repositories

If you have a lot of repositories in your Azure DevOps team, you need an easy way to clone and/or update your local Git repositories.

You first need to install the Az-cli available at: https://docs.microsoft.com/cli/azure/install-azure-cli

Once installed, please run the following in a console:
az extension add --name azure-devops

You also need to login, just once. Run the following in a console:
az login

Create the following files:
@echo off
powershell .\sync.ps1 C:\YourWorkFolder
pause
view raw sync.cmd hosted with ❤ by GitHub
param([parameter(Mandatory = $true)] [string]$targetDirectory)
pushd .
cd $targetDirectory
# https://docs.microsoft.com/cli/azure/install-azure-cli
# az extension add --name azure-devops
# az login
az devops configure --defaults project=XXXXXXXXXX
az devops configure --defaults organization=https://dev.azure.com/XXXXXXXXXX/
$repos = (az repos list | ConvertFrom-Json) | Select remoteUrl,name
$repos | ForEach {
try
{
if (Test-Path $_.name)
{
pushd .
cd $_.name
git pull origin master
popd
}
else
{
git clone $_.remoteUrl $_.name
}
} catch {
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
write-host ("Error detected. Continuing." )
write-host ("$ErrorMessage")
write-host ("$FailedItem")
}
}
popd
view raw sync.ps1 hosted with ❤ by GitHub


Replace C:\YourWorkFolder with the root folder of all your repos.
Replace XXXXXXXXXX with the name of your organisation and project.

For example:
az devops configure --defaults project=TestProject
az devops configure --defaults organization=https://dev.azure.com/TestCompany/


Run the sync.cmd file, and that is it. It will clone new repos if you don't already have them, and will update your repos if you already have some.

Popular posts from this blog

Service Broker sys.transmission_queue clean up

Execution of user code in the .NET Framework is disabled

AWS DynamoDB vs Azure CosmosDB vs Azure Table Storage pricing comparison