How To Merge Two Branches In Azure DevOps

In this article, I’m going to walk you through the professional standards of how to merge two branches in Azure DevOps. We won’t just look at the “buttons to click”; we will look at the architecture of a clean merge.

How To Merge Two Branches In Azure DevOps

Here I will demonstrate the two primary ways to merge code: the Pull Request Method (the enterprise standard) and the Manual Command Line Method.

Method 1: The Pull Request (Recommended for Teams)

You should never merge directly into your protected branches. You use a Pull Request.

Step 1: Navigate to Repos

Log in to your Azure DevOps organization (e.g., dev.azure.com/YourCompany). Navigate to the project and select Repos from the left-hand sidebar, then click on Pull Requests. Check out the screenshot below for your reference.

How To Merge Two Branches In Azure DevOps

Step 2: Initialize the Pull Request

Click the New Pull Request button. You will see two dropdown menus.

  • Source Branch: This is your feature branch (e.g., users/john-doe/tax-calculator-update).
  • Target Branch: This is where the code is going (usually main or develop).

Step 3: Documentation and Reviewers

Enter a descriptive title. Instead of “Fixing bug,” use “Fixed calculation error in New York State tax logic.” * Description: Use bullet points to explain what changed and why.

  • Reviewers: Add your teammates. In many US firms, we require at least two approvals for production code.
  • Work Items: Link the specific Azure Board task so stakeholders can track progress.

Step 4: The Review Phase

Your peers will review the code. They might leave comments or request changes. Azure DevOps allows for “threaded conversations,” making it easy to resolve disputes over code style or logic.

Step 5: Complete the Merge

Once the “Required” checks pass (Build Validation, Minimum Reviewers, Work Item Linking), click Complete.

You will be presented with Merge Types:

Merge TypeWhat it DoesWhen to Use It
Basic Merge (No-FF)Keeps all individual commits and creates a merge commit.When you need a full, granular history.
Squash MergeCondenses all commits into one single commit on the target branch.Best Practice for keeping the Main history clean.
Rebase and Fast-ForwardReplays your commits on top of the target branch.When you want a perfectly linear history.
Semi-Linear MergeRebase first, then create a merge commit.A middle ground for strict traceability.

Method 2: The Manual Merge (Command Line)

Sometimes, you need to merge branches that aren’t protected, or you’re working on a small internal tool in a “Solo Dev” capacity in a place like a home office in Portland.

Step 1: Switch to Target Branch

Open your terminal (PowerShell or Bash) and move to the branch you want to merge into.

Bash

git checkout main

Step 2: Pull Latest

Ensure your local main is identical to the Azure DevOps server.

Bash

git pull origin main

Step 3: Execute the Merge

Merge your feature branch into your current branch.

Bash

git merge feature/new-login-ui

Step 4: Push to Azure DevOps

Once the merge is completed locally, send the updated main branch back to the cloud.

Bash

git push origin main

Handling Merge Conflicts

Merge conflicts happen when two developers edit the same line of the same file. Azure DevOps will block the merge until this is resolved.

How to Resolve Conflicts

  1. Identify the Files: Azure DevOps will list the conflicting files in the PR “Conflicts” tab.
  2. The “Web Editor” vs. “Local”: For simple conflicts, you can use the Azure DevOps “Conflict Resovler” extension. For complex logic, resolve it locally in VS Code or Visual Studio.
  3. Choosing a Path: You must decide:
    • Keep Source: Use your changes.
    • Keep Target: Use what is already in Main.
    • Hybrid: Manually combine the logic.

Check out How to Resolve Merge Conflicts in Azure DevOps

Best Practices

To maintain a “Senior” level of authority in your DevOps role, you should implement these policies within your Azure DevOps project settings.

1. Branch Policies

Don’t trust; verify. Enable Branch Policies on your Main branch:

  • Require a minimum number of reviewers.
  • Check for linked work items.
  • Require a successful build. This ensures that nobody merges code that “breaks the build.”

2. The “Squash Merge” Standard

In high-velocity American tech companies, we hate “cluttered” histories. If Sarah makes 45 commits while building a feature, we don’t want those 45 messages in our production log. We use Squash Merge to turn those 45 commits into one clean, meaningful entry.

3. Delete Source Branch

Always check the box “Delete [source branch] after merging.” This prevents your repository from looking like an abandoned construction site with hundreds of “stale” branches.

4. Naming Conventions

Adopt a standardized naming convention across your team:

  • feat/ for new features.
  • bug/ for fixes.
  • docs/ for documentation changes.
  • refactor/ for code cleanup.

Troubleshooting Common Merge Errors

Here are the most frequent issues encountered in Azure DevOps:

  • Error: “Identity not authorized”: Usually a permission issue. Ensure your user account has “Contribute” permissions on the target branch.
  • Error: “Pre-condition failed”: A branch policy is likely being violated. Check if your build pipeline failed or if you forgot to link a work item.
  • Error: “Multiple merge bases”: This happens in complex “criss-cross” merges. The best fix is usually to rebase your branch against the target branch.

Conclusion:

Merging two branches in Azure DevOps is more than a technical command; it is a milestone in the journey of your software. By moving away from manual merges and embracing the Azure DevOps Pull Request workflow, you ensure that your code is vetted, tested, and documented.

Whether you are managing a small team or a massive distributed workforce, these practices will keep your “Main” branch stable and your deployments frequent.

You may also like the following articles:

Azure Virtual Machine

DOWNLOAD FREE AZURE VIRTUAL MACHINE PDF

Download our free 25+ page Azure Virtual Machine guide and master cloud deployment today!