Azure Repos Git: A Comprehensive Guide

Welcome to the official Microsoft Developer Network (MSDN) tutorial on using Git with Azure Repos. Azure Repos provides unlimited, cloud-hosted private Git repositories for your project. This tutorial will guide you through the essential concepts and commands to effectively manage your code using Git within the Azure ecosystem.

Getting Started with Azure Repos Git

Before diving into commands, ensure you have an Azure DevOps organization and a project set up. You can create one for free. Once your project is ready, you'll be able to create new Git repositories or connect to existing ones.

To get started, navigate to your Azure DevOps project, select "Repos" from the left-hand navigation, and then choose "Files". Here you'll find options to clone your repository to your local machine or create a new one.

Create Your First Azure Repo

Mastering Basic Git Commands

Git is a powerful version control system. Understanding its core commands is crucial for efficient development. Let's explore some of the most frequently used ones:

git init

Initializes a new, empty Git repository in the current directory. This command creates a hidden .git subdirectory that contains all the necessary repository files.

git init

git clone

Copies an existing Git repository from a remote source (like Azure Repos) to your local machine. You'll typically use the HTTPS or SSH URL provided by Azure Repos.

git clone https://dev.azure.com/your-organization/your-project/_git/your-repo-name

git add

Stages changes in your working directory, preparing them to be included in the next commit. You can add specific files or all modified files.

git add .

Or for a specific file:

git add README.md

git commit

Records the staged changes to the repository's history. Each commit represents a snapshot of your project at a specific point in time. A descriptive commit message is highly recommended.

git commit -m "Add initial project files"

git push

Uploads your local commits to a remote repository (e.g., Azure Repos). This is how you share your changes with others and back them up.

git push origin main

git pull

Fetches changes from a remote repository and merges them into your current local branch. This keeps your local repository synchronized with the remote.

git pull origin main
Tip: Regularly commit and push your changes to avoid losing work and to maintain a clear history.

Branching and Merging Strategies

Branching allows you to diverge from the main line of development and continue to do the unrelated work without disrupting the main branch. Merging combines those divergent branches back into one.

Key commands:

  • git branch <branch-name>: Create a new branch.
  • git checkout <branch-name>: Switch to a branch.
  • git merge <branch-name>: Merge a branch into your current branch.

Azure Repos also supports Pull Requests, a workflow that facilitates code review before merging changes.

Working with Remote Repositories

Azure Repos acts as your remote repository. You can manage your remotes using commands like:

  • git remote -v: Lists all configured remote repositories.
  • git remote add <name> <url>: Adds a new remote.
  • git remote remove <name>: Removes a remote.

Exploring Advanced Git Concepts

As you become more comfortable with Git, you can explore advanced features such as:

  • Rebasing: Rewriting commit history.
  • Stashing: Temporarily saving uncommitted changes.
  • Git Hooks: Automating tasks based on Git events.
  • Git Ignore: Specifying files that Git should intentionally ignore.
Note: For detailed information on advanced Git commands and workflows, refer to the official Git documentation or explore further MSDN resources.

Continue your learning journey by exploring Azure Pipelines for CI/CD integration.

Next: Azure Pipelines