DevOps | Scripts | Automation

Azure DevOps

How to clone Azure DevOps Repo?

Overview

To clone the Azure DevOps repo, there are various methods provided by Microsoft to contribute to the team’s code. In the cloning technique, we download the entire repo into the local machine and we contribute.

Cloning is a widely popular centralized technique as one just have to download the code and once the file is modified, user has to commit, push and sync the repo so that the code will be merged.

Although it’s a broad topic, how the repository can be committed, pushed, or synced with the Pull Request (PR)? In this article, we will just focus on the Cloning techniques that beginners can also understand.

Methods available for Cloning

There are three primary methods for cloning the ADO repo.

  • Https-based cloning
  • SSH-based cloning
  • Cloning with Personal Access Token (PAT).

We will go through each method but considering the length of article, I will explain PAT token based clone in the next article.

1. https-based method

To clone with https based method, you need to have git installed on your machine. You can download the git package from this website: https://git-scm.com/download

Once you downloaded the git package for your respective OS, git commands and git credential manager (used for authentication) will be installed.

Now open your Azure repo, and click on the Clone button.

Once you click on the clone button, select HTTPS (default) button and https clone URL will be there. Click on the “Generate Git Credential” button.

Alright, now you have git installed and credentials generated. Copy the https URL, open any terminal (cmd or PowerShell), and type the command below. Before running this command make sure the desired destination directory should be created.

git clone  git clone https://chiragnagrekar@dev.azure.com/xxxxxxx/theAutomationCode/_git/xxxxx C:\Repository\

The above command will clone the repository in the C:\Repository folder. As this is a private project, the git credential manager will be opened for Authentication.

Enter your credentials for the first time for the account used for git and enter the git password generated through the clone window.

It will clone the repository to the C:\Repository folder. Ignore the certificate warning if you are receiving it.

In this method, every time you run the commit and push command of git, you have to enter credentials. The below SSH method overcomes this problem.

2. SSH Based method.

The second known method is SSH based method. In this method, you need to generate the private and public SSH keys for the machine, and the public key is configured to Azure DevOps, so once pairs are matched, authentication works.

PS: This method also requires the git command line tool as mentioned in method 1.

The problem with the HTTP-based method is if you are using a programmatic way to clone the repo, update the code, and push the code back to the repo then https based method asks for credentials whenever you commit and push the code. To overcome this, SSH-based authentication is used.

So the combination of Keys (Private and Public) is used for authentication. There are multiple tools to generate SSH keys like OpenSSH, KeyGen, Putty, etc. If you are using the Windows operating system then it has default OpenSSH installed.

Use PowerShell or cmd to run the ssh-keygen.exe command. The default install path for OpenSSH is C:\windows\System32\OpenSSH\ssh-keygen.exe.

Open PowerShell or cmd in administrator mode and follow the below steps.

  • ssh-keygen
  • Enter file location (Default path: %userprofile%\.ssh\, you can change)
  • Enter passphrase.
  • Two files will be generated in the path you mentioned (id_rsa and id_rsa.pub)

The below files will be generated. PUB file is the public key file and the other one is the private key file.

  • Copy the content of id_rsa.pub in the Azure DevOps Public Key profile. Open the .pub file (any editor) and copy the content of it.
  • Now, login to Azure DevOps and open user settings -> SSH Public Keys -> +New Key
  • Once you click on +New Key, the below window will be opened.
  • You can see the below Public Key will be added to the ADO dashboard.
  • That’s it. You are now done with the SSH keys configuration and to test if your machine is configured properly to connect with git with SSH run the below command,
ssh -T git@ssh.dev.azure.com

You will receive a message like remote: Shell access is not supported then SSH is configured properly. If not then use this troubleshooting page.

  • Next, Open Repo and click on the clone button, and select SSH.
  • Open PowerShell and type the copied SSH URL.
git clone git@ssh.dev.azure.com:v3/xaxxxxxx/theAutomationCode/xbxxxxxxx

That’s it. You will able to get a download (Clone) the repository.

Thank you for reading this article, we will cover PAT-based cloning in the next article.

Loading

One thought on “How to clone Azure DevOps Repo?

Comments are closed.