Introduction

Azure Repos is a set of version control tools that you can use to manage your code. It provides two types of version control systems: Git (distributed) and Team Foundation Version Control (TFVC, centralized). This module will focus on Git, as it is the most widely used version control system today.

Key Concepts

  1. Version Control Systems (VCS):

    • Git: A distributed version control system where every developer has a full copy of the repository.
    • TFVC: A centralized version control system where there is a single server that contains all the versioned files.
  2. Repositories:

    • A repository (repo) is a storage space where your project lives. It can be local to a folder on your computer or a storage space on Azure Repos.
  3. Branches:

    • Branches allow you to work on different parts of a project simultaneously. The default branch in Git is usually called main or master.
  4. Commits:

    • A commit is a snapshot of your repository at a specific point in time. It includes a message describing the changes made.
  5. Pull Requests:

    • Pull requests are a way to propose changes to the codebase. They allow team members to review and discuss the changes before they are merged into the main branch.

Setting Up Azure Repos

Step 1: Create a New Repository

  1. Navigate to your Azure DevOps project.
  2. Select Repos from the left-hand menu.
  3. Click on New repository.
  4. Fill in the repository name and select the type of version control (Git or TFVC).
  5. Click Create.

Step 2: Clone the Repository

  1. Copy the URL of your new repository.
  2. Open your terminal or command prompt.
  3. Use the git clone command followed by the repository URL:
    git clone https://dev.azure.com/yourorganization/yourproject/_git/yourrepository
    

Step 3: Add Files and Make Your First Commit

  1. Navigate to the cloned repository folder:
    cd yourrepository
    
  2. Create a new file, for example, README.md:
    echo "# My Project" > README.md
    
  3. Add the file to the staging area:
    git add README.md
    
  4. Commit the file to the repository:
    git commit -m "Initial commit"
    
  5. Push the commit to Azure Repos:
    git push origin main
    

Working with Branches

Creating a New Branch

  1. Create a new branch:
    git checkout -b feature-branch
    
  2. Make changes to your code and commit them:
    git add .
    git commit -m "Added new feature"
    
  3. Push the branch to Azure Repos:
    git push origin feature-branch
    

Merging Branches with Pull Requests

  1. Navigate to your Azure DevOps project.
  2. Select Repos > Branches.
  3. Find your branch and click on New pull request.
  4. Fill in the details and create the pull request.
  5. Reviewers can now review the changes and approve the pull request.
  6. Once approved, the changes can be merged into the main branch.

Practical Exercise

Exercise: Create and Merge a Feature Branch

  1. Create a new repository in Azure Repos.
  2. Clone the repository to your local machine.
  3. Create a new branch called feature-branch.
  4. Add a new file called feature.txt with some content.
  5. Commit and push the changes to the feature-branch.
  6. Create a pull request to merge feature-branch into the main branch.
  7. Approve and merge the pull request.

Solution

  1. Create a new repository in Azure Repos.
  2. Clone the repository:
    git clone https://dev.azure.com/yourorganization/yourproject/_git/yourrepository
    cd yourrepository
    
  3. Create a new branch:
    git checkout -b feature-branch
    
  4. Add a new file:
    echo "This is a new feature" > feature.txt
    git add feature.txt
    git commit -m "Added feature.txt"
    git push origin feature-branch
    
  5. Create a pull request in Azure DevOps.
  6. Approve and merge the pull request.

Common Mistakes and Tips

  • Forgetting to commit changes: Always remember to commit your changes before pushing them to the repository.
  • Not pulling the latest changes: Before starting new work, pull the latest changes from the main branch to avoid conflicts.
  • Descriptive commit messages: Use clear and descriptive commit messages to make it easier to understand the history of changes.

Conclusion

In this module, you learned about Azure Repos and how to use it for version control with Git. You set up a new repository, created branches, made commits, and used pull requests to merge changes. These skills are fundamental for collaborative software development and will be essential as you continue to work with Azure DevOps.

© Copyright 2024. All rights reserved