Part 2: Managing Branches and Pull Requests in Azure DevOps

Part 2: Managing Branches and Pull Requests in Azure DevOps

Part 1: Configuring Git Client and Managing Branches in Azure DevOps (Link to Part 1)

In this part, we’ll explore how to manage branches and pull requests directly from Azure Repos, including creating, deleting, restoring, and locking branches, as well as using tags, creating repositories, and managing pull requests.

Step 1: Creating a New Branch in Azure Repos

Previously, we created and managed branches from VS Code. Now, let’s see how to do it from Azure Repos.

  1. Navigate to your project in Azure DevOps.

  2. Go to Repos and select Branches from the left-hand menu.

  3. Click on New Branch at the top of the branches list.

  4. Name your branch (e.g., release) and base it on master. Optionally, link it with a work item or leave it blank.

  5. Hit Create to generate the new branch.

Once the new branch is created, it will appear under the list of branches in Azure Repos.

Step 2: Syncing the New Branch with VS Code

After creating the new branch in Azure Repos, you can bring it into your local environment:

  1. Open VS Code and use Ctrl + Shift + P to open the command palette.

  2. Type Git Fetch and select Git Fetch to fetch the latest remote branches.

  3. You’ll now see the release branch listed in VS Code as origin/release.

  4. To create a local branch for the fetched remote branch, select origin/release, and a local branch will be created with the same name (release).

Now, both the local and remote branches are synced.

Step 3: Restoring a Deleted Branch

If a branch gets deleted from the remote repository but you want to restore it:

  1. Go back to Azure Repos, and under Branches, search for the deleted branch name (e.g., release).

  2. Ensure you type the exact branch name, as it’s case-sensitive.

  3. Once you find it in the Deleted Branches section, click on the three dots (...) next to the branch and select Restore Branch.

  4. The branch will be restored and available again in both Azure Repos and your local environment.

Step 4: Locking a Branch

To prevent changes to a branch, especially during critical times like a code freeze, you can lock the branch:

  1. In Azure Repos, go to Branches and find the branch you want to lock (e.g., release).

  2. Click on the three dots (...) next to the branch name and select Lock.

  3. The branch will now be read-only, and no one can push new changes to it.

To unlock the branch, simply go back to the same menu and select Unlock.

Step 5: Using Tags for Releases

Tags are used to mark specific points in the commit history, often for releases. Let’s create a new tag:

  1. In Azure Repos, click on the Tags tab under Repos.

  2. You’ll see existing tags, if any, and their associated commit IDs.

  3. To create a new tag, click New Tag, name it (e.g., 1.1), and base it on the master branch.

  4. Add a description such as "Release 1.1" and click Create.

Now, this tag will mark the latest commit on the master branch. If you inspect the tag, you’ll see the changes that were finalized before this release.

Step 6: Creating a New Repository

To create a new repository in Azure DevOps:

  1. In your project, click the + button next to Repos in the left-hand menu.

  2. Choose Create New Repository and name it (e.g., test).

  3. Select Git as the version control system, and check the box to include a README file.

  4. Hit Create to set up the new repository.

You can switch between repositories from the Repos dropdown menu, import existing repositories, or manage and delete them as needed.

Step 7: Creating and Managing Pull Requests

Pull requests (PRs) ensure that changes are reviewed before merging into the main branch. This helps maintain code quality when working in teams. Here’s how to create a pull request:

  1. In VS Code, make changes in your branch (e.g., release), stage the changes, and commit them with a message (e.g., "Pull Request Demo").

  2. Sync the changes by clicking Sync Changes.

  3. In Azure DevOps, navigate to Repos > Pull Requests and click New Pull Request.

  4. Choose the release branch as the source and master as the target branch. Add a title, description, and select yourself or others as reviewers.

  5. Link the relevant work item, such as "As a customer, I want to view the new tutorial," to ensure it’s tracked along with the PR.

  6. Click Create to open the pull request.

Once the pull request is open, you can review the changes:

  1. Go to the Files tab to see the modified files.

  2. Check the Commits tab for commit history.

  3. As a reviewer, you can approve the changes, approve with suggestions, or reject the PR. Add a comment (e.g., "LGTM" – Looks Good To Me) before finalizing the review.

After the review, complete the pull request:

  1. Choose the Merge Type (e.g., standard merge).

  2. Select Complete associated work items to mark the linked work items as done once the PR is merged.

  3. Click Complete Merge. The code will be merged into the master branch, and the PR will be closed.

To verify, navigate to Repos > Files and ensure the changes are reflected in the codebase. You can also check that the work item is marked as completed.

Step 8: Enforcing Pull Request and Branch Policies

Azure Repos provides several security options to enhance the integrity of your branches. These can be enforced via branch policies:

  1. Go to Project Settings in Azure DevOps and select Repos under Repositories.

  2. Click on Repository Policies for the repository in which you want to enforce rules.

  3. Set branch-specific policies (e.g., master):

    • Minimum number of reviewers: Require at least 2 reviewers for each pull request.

    • Linked work items: Ensure that each pull request is linked to a work item.

    • Automatically include reviewers: Add default reviewers (e.g., yourself) for specific branches.

After saving these policies, they will automatically apply to all pull requests targeting that branch.


By following the steps outlined in Part 2, you should now be comfortable managing branches, handling pull requests, and setting up repository security in Azure DevOps. Make sure to practice these hands-on steps to solidify your understanding!