Complete Guide to Setting Up Azure Repos with Git for Version Control
In the world of modern software development, version control is essential. It allows developers to manage code changes, collaborate seamlessly, and track the evolution of a project over time. Azure Repos, a part of Microsoft’s Azure DevOps suite, offers a powerful, scalable, and secure platform for version control using Git repositories. In this guide, we’ll walk through the steps to set up Azure Repos, get started with Git, and leverage version control to streamline your development process.
What are Azure Repos?
Azure Repos is a set of version control tools hosted in the cloud, part of Azure DevOps, which helps developers manage their code and track its history. It supports both Git and Team Foundation Version Control (TFVC), but Git is the more commonly used version control system due to its distributed nature and integration with various tools.
Azure Repos allows developers to:
Store and manage code in Git repositories.
Collaborate on code changes with branch strategies.
Integrate with CI/CD pipelines to automate testing and deployments.
Set up access control and permissions for team members.
Use pull requests for code review and approval workflows.
Now, let’s dive into how you can set up Azure Repos for your project using Git.
Prerequisites
Before you begin, you’ll need:
Azure DevOps Organization: If you don’t have one already, you can create an Azure DevOps account at dev.azure.com.
Git: Ensure that Git is installed on your local machine. If you don't have it installed, you can download it from git-scm.com.
Visual Studio or another Git-compatible IDE (optional, but helpful for code editing and integration).
Step 1: Create a Project in Azure DevOps
The first step is to create a new project in Azure DevOps to house your repositories.
Log in to Azure DevOps: Go to Azure DevOps and log in with your credentials.
Create a New Project:
Click on
New Project
from the dashboard.Give your project a name, and optionally, a description.
Choose the visibility (public or private) and the version control system you want (Git or TFVC). Select Git if it’s not pre-selected.
Click Create.
After your project is created, you’ll be taken to the Project Dashboard, where you can manage repositories, pipelines, boards, and more.
Step 2: Create a New Repository
Once your project is set up, the next step is to create a Git repository.
Navigate to Repos:
On the left sidebar, click on Repos.
Select Files from the dropdown.
Create a New Repository:
If there are no existing repositories, you’ll see a prompt to create a new repository. Click on
New repository
.Name your repository (e.g.,
my-app
).Choose the repository type (Git or TFVC) and select Git.
Click Create.
After the repository is created, you’ll be able to see it in the Files section, along with instructions to push your local code to the remote repository.
Step 3: Clone the Repository Locally
To start working with the repository on your local machine, you'll need to clone it.
Get the Clone URL:
In Azure DevOps, go to Repos > Files.
On the right-hand side, click on the Clone button.
Copy the HTTPS URL provided. This is the URL you’ll use to clone the repository locally.
Clone the Repository Using Git:
Open a terminal (or Git Bash) on your local machine.
Navigate to the folder where you want to store the repository.
Run the following command, replacing
<URL>
with the copied clone URL:git clone <URL>
This command will create a local copy of the repository on your machine.
Step 4: Add Your Code and Make the First Commit
Now that you’ve cloned the repository, it’s time to start adding your code.
Add Your Code:
Navigate to the local repository folder on your computer.
Add your code files to the repository directory (e.g., create new files or copy existing files).
Stage and Commit Your Changes:
Use the following commands to stage and commit your changes:
git add . git commit -m "Initial commit"
The git add .
command stages all new and modified files, and git commit -m
records the changes with a message.
Push Changes to Azure Repos:
After committing your changes, push them to Azure Repos using the following command:
git push origin master
This command pushes your local commits to the master
branch in your Azure DevOps repository.
Step 5: Collaborate with Teams Using Branches and Pull Requests
One of the most powerful features of Git is branching, which allows teams to work on different features or fixes simultaneously without interfering with each other's work.
Creating a Branch
Create a Branch: To work on a new feature, create a new branch by running:
git checkout -b new-feature
This creates and switches to a new branch called
new-feature
.Make Changes: Now, you can make changes to the code in this new branch.
Push the Branch: Once your changes are made, push the branch to Azure Repos:
git push origin new-feature
Creating a Pull Request (PR)
Once your feature branch is ready and pushed to Azure Repos, you can create a Pull Request (PR) to merge your changes into the master
branch.
Go to Azure Repos:
In Azure DevOps, go to Repos > Pull Requests.
Click on
New Pull Request
.Choose the base branch (e.g.,
master
) and the source branch (e.g.,new-feature
).Add a title and description for your PR.
Select reviewers to review your changes.
Review and Merge:
- Reviewers will check the code and provide feedback. Once the PR is approved, you can merge the branch into the main codebase.
Step 6: Set Up Continuous Integration (CI)
After you’ve set up version control and collaboration, the next step is to automate your workflow using Continuous Integration (CI). Azure DevOps makes it easy to set up CI pipelines to automatically build and test your code on every commit or PR.
Navigate to Pipelines: In your Azure DevOps project, go to Pipelines.
Create a New Pipeline: Click on New Pipeline and follow the steps to connect your repository and choose a build configuration (e.g., using YAML or a classic editor).
Configure Your Pipeline: Set up your build tasks (e.g., compiling code, running tests) and save your pipeline.
Now, every time code is pushed to the repository, the pipeline will automatically trigger, ensuring that your code is always tested and ready for deployment.
Conclusion
Setting up Azure Repos for version control with Git is a straightforward process that enables you and your team to effectively manage code changes, collaborate, and integrate with modern DevOps practices. By following the steps outlined in this guide, you can easily start using Git with Azure Repos to track your code, improve collaboration, and automate your workflows.
Version control with Azure Repos and Git is a powerful combination that scales with your team’s needs and integrates seamlessly with other Azure DevOps tools like Pipelines, Boards, and Test Plans. Whether you’re working solo or as part of a large team, Azure Repos can help you manage your codebase and streamline your development lifecycle. Happy coding!
Subscribe to my newsletter
Read articles from Jayachandra Rapolu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by