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
-
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.
-
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.
-
Branches:
- Branches allow you to work on different parts of a project simultaneously. The default branch in Git is usually called
main
ormaster
.
- Branches allow you to work on different parts of a project simultaneously. The default branch in Git is usually called
-
Commits:
- A commit is a snapshot of your repository at a specific point in time. It includes a message describing the changes made.
-
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
- Navigate to your Azure DevOps project.
- Select Repos from the left-hand menu.
- Click on New repository.
- Fill in the repository name and select the type of version control (Git or TFVC).
- Click Create.
Step 2: Clone the Repository
- Copy the URL of your new repository.
- Open your terminal or command prompt.
- 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
- Navigate to the cloned repository folder:
cd yourrepository
- Create a new file, for example,
README.md
:echo "# My Project" > README.md
- Add the file to the staging area:
git add README.md
- Commit the file to the repository:
git commit -m "Initial commit"
- Push the commit to Azure Repos:
git push origin main
Working with Branches
Creating a New Branch
- Create a new branch:
git checkout -b feature-branch
- Make changes to your code and commit them:
git add . git commit -m "Added new feature"
- Push the branch to Azure Repos:
git push origin feature-branch
Merging Branches with Pull Requests
- Navigate to your Azure DevOps project.
- Select Repos > Branches.
- Find your branch and click on New pull request.
- Fill in the details and create the pull request.
- Reviewers can now review the changes and approve the pull request.
- Once approved, the changes can be merged into the main branch.
Practical Exercise
Exercise: Create and Merge a Feature Branch
- Create a new repository in Azure Repos.
- Clone the repository to your local machine.
- Create a new branch called
feature-branch
. - Add a new file called
feature.txt
with some content. - Commit and push the changes to the
feature-branch
. - Create a pull request to merge
feature-branch
into themain
branch. - Approve and merge the pull request.
Solution
- Create a new repository in Azure Repos.
- Clone the repository:
git clone https://dev.azure.com/yourorganization/yourproject/_git/yourrepository cd yourrepository
- Create a new branch:
git checkout -b feature-branch
- 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
- Create a pull request in Azure DevOps.
- 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.