Git Azure DevOps
Introduction
Azure DevOps (formerly known as Visual Studio Team Services or VSTS) provides a powerful platform for managing the complete development lifecycle, and Git serves as its primary version control system. This integration allows developers to manage source code, track changes, collaborate effectively, and automate build and deployment processes.
In this tutorial, we'll explore how to use Git with Azure DevOps, covering everything from setting up repositories to implementing advanced workflows for team collaboration. Whether you're working solo or in a team, these tools will streamline your development process and help maintain code quality.
Prerequisites
Before we start, make sure you have:
- Basic understanding of Git concepts (commits, branches, merges)
- An Azure DevOps account (free tier available)
- Git installed on your local machine
- Visual Studio, VS Code, or your preferred IDE
Setting Up Git with Azure DevOps
Step 1: Create a New Project in Azure DevOps
- Log in to your Azure DevOps account at dev.azure.com
- Click on "New project"
- Enter a project name and description
- Select "Git" as the version control system
- Click "Create project"
Step 2: Initialize a Git Repository
When your project is created, Azure DevOps automatically initializes a Git repository for you. You can either:
- Clone this empty repository to start fresh, or
- Push an existing local Git repository to Azure DevOps
Let's look at both approaches:
Cloning an Empty Repository
# Navigate to your desired folder
git clone https://[email protected]/organization-name/project-name/_git/repository-name
cd repository-name
# Create some files
echo "# My Project" > README.md
git add README.md
git commit -m "Initial commit"
git push -u origin main
Pushing an Existing Repository
# Navigate to your existing git repository
cd my-existing-project
# Add the Azure DevOps remote
git remote add azure https://[email protected]/organization-name/project-name/_git/repository-name
# Push your code to Azure DevOps
git push -u azure --all
Working with Branches in Azure DevOps
Azure DevOps follows Git's branching model, allowing you to create and manage branches both through the web interface and using Git commands.
Creating a Branch Using the Web Interface
- Navigate to your repository in Azure DevOps
- Click on "Branches" in the left sidebar
- Click "New branch"
- Enter a name (e.g.,
feature/login-page
) - Select the source branch (usually
main
ormaster
) - Click "Create"
Working with Branches Using Git Commands
# Create and switch to a new branch
git checkout -b feature/login-page
# Make changes, add, and commit
git add .
git commit -m "Implement login page"
# Push the branch to Azure DevOps
git push -u origin feature/login-page
Pull Requests in Azure DevOps
Pull Requests (PRs) allow you to review code before merging branches. They're essential for quality control and team collaboration.
Creating a Pull Request
- After pushing your branch, navigate to your repository
- Click "Create a pull request" (often appears as a notification)
- Fill in the details:
- Title: Brief description of changes
- Description: Detailed explanation of changes
- Reviewers: Team members who should review the code
- Click "Create"
Here's a diagram showing the PR workflow:
Reviewing and Completing Pull Requests
As a reviewer, you can:
- Add comments on specific lines of code
- Suggest changes
- Approve the PR or request changes
Once approved, to complete the PR:
- Click "Complete"
- Choose merge options (merge, squash, or rebase)
- Optionally check "Delete source branch"
- Click "Complete merge"
CI/CD with Azure Pipelines
One of the most powerful features of Azure DevOps is Azure Pipelines, which allows you to automate builds and deployments.
Creating a Basic CI Pipeline
- In your project, click on "Pipelines" > "New pipeline"
- Select "Azure Repos Git" as your code location
- Select your repository
- Choose a template (e.g., "Node.js" for a JavaScript project)
- Save your pipeline YAML file (you can customize it as needed)
Here's a simple YAML pipeline example for a Node.js project:
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool@0
inputs:
versionSpec: '16.x'
displayName: 'Install Node.js'
- script: |
npm install
npm run build
displayName: 'npm install and build'
- script: |
npm test
displayName: 'Run tests'
Advanced Git Features in Azure DevOps
Branch Policies
Branch policies protect important branches by enforcing certain rules before merging:
- Go to "Repos" > "Branches"
- Find your main branch and select "..." > "Branch policies"
- Set requirements such as:
- Minimum number of reviewers
- Linked work items
- Build validation
- Comment resolution
Git Hooks
Azure DevOps supports server-side Git hooks through branch policies, but you can also use client-side hooks for additional checks:
Create a .git/hooks/pre-commit
file in your local repository:
#!/bin/sh
# Example: Prevent committing to the main branch directly
branch="$(git rev-parse --abbrev-ref HEAD)"
if [ "$branch" = "main" ]; then
echo "You can't commit directly to the main branch!"
exit 1
fi
# Run linting
npm run lint
# Exit with the status of the lint command
exit $?
Make the hook executable:
chmod +x .git/hooks/pre-commit
Working with Git Tags
Tags are useful for marking release points. In Azure DevOps, you can manage tags through the web interface or Git commands:
# Create a new tag
git tag -a v1.0.0 -m "Version 1.0.0"
# Push tags to Azure DevOps
git push --tags
Best Practices for Git with Azure DevOps
- Use a consistent branching strategy: Consider Git Flow or GitHub Flow
- Write meaningful commit messages: Follow the format "type: subject" (e.g., "feat: add login functionality")
- Keep pull requests small: Smaller PRs are easier to review
- Use work items: Link commits and PRs to Azure DevOps work items
- Set up branch policies: Protect your main branches
- Automate testing: Configure CI pipelines to run tests automatically
- Regular housekeeping: Delete merged branches periodically
Common Workflow Example
Let's walk through a typical workflow using Git and Azure DevOps:
-
Create a work item:
- In Azure DevOps, create a new task or user story
-
Create a feature branch:
bashgit checkout main
git pull
git checkout -b feature/user-authentication -
Make changes and commit:
bash# Make changes to files
git add .
git commit -m "feat: implement user login form" -
Push to Azure DevOps:
bashgit push -u origin feature/user-authentication
-
Create a Pull Request:
- Navigate to the repository in Azure DevOps
- Create a PR from
feature/user-authentication
tomain
- Link the work item
- Add reviewers
-
Address review comments:
bash# Make requested changes
git add .
git commit -m "refactor: address review comments"
git push -
Complete the PR:
- Once approved, complete the PR in Azure DevOps
- The pipeline will automatically build and test your code
-
Sync local repository:
bashgit checkout main
git pull
Troubleshooting Common Issues
Authentication Problems
If you're having trouble authenticating, try using a Personal Access Token (PAT):
- Go to Azure DevOps > User settings > Personal access tokens
- Create a new token with appropriate permissions
- Use the token in place of your password when Git prompts for credentials
Merge Conflicts
When you encounter merge conflicts:
# Pull the latest changes from the target branch
git checkout main
git pull
# Switch back to your feature branch
git checkout feature/my-feature
# Merge main into your feature branch
git merge main
# Resolve conflicts manually, then
git add .
git commit -m "resolve merge conflicts"
git push
Summary
In this tutorial, we've covered:
- Setting up Git with Azure DevOps
- Creating and working with branches
- Using pull requests for code reviews
- Implementing CI/CD with Azure Pipelines
- Advanced Git features in Azure DevOps
- Best practices and common workflows
Git integration with Azure DevOps provides a robust platform for version control, collaboration, and automation. By following the practices outlined in this guide, you'll be well on your way to more efficient and effective development workflows.
Additional Resources
- Azure DevOps Documentation
- Git Documentation
- Learn Git Branching - Interactive Git tutorial
Exercises
- Create a new Azure DevOps project and push an existing Git repository to it.
- Create a feature branch, make changes, and submit a pull request.
- Set up a basic CI/CD pipeline that runs tests on pull requests.
- Configure branch policies for your main branch.
- Create a release using Git tags and Azure DevOps release pipelines.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)