Implementing Branch Merging Restrictions in Azure DevOps and GitHub


LearnAzureDevOps-O5

Implementing Branch Merging Restrictions in Azure DevOps and GitHub

Branch merging restrictions ensure that only authorized users or approved workflows can merge branches into specific branches like main, develop, or any protected branch. Below is how you can implement these restrictions in Azure DevOps and GitHub.

1. Azure DevOps – Implement Branch Merging Restrictions

Steps to Implement Branch Merging Restrictions in Azure DevOps

  1. Set Up a Repository:

    • Navigate to Project Settings > Repositories > Branches.

    • Select the branch you want to protect (e.g., main, develop).

  2. Enable Branch Protection:

    • Click Edit next to the selected branch.

    • Enable the following protections:

      • Require a minimum number of reviewers: Specify the number of required approvers.

      • Require a branch policy: Choose policies such as build validation, status checks, and others.

      • Restrict who can push: Limit pushing to specific users or groups.

  3. Configure Build Validation: Set up CI pipelines to validate the branch by enabling build pipelines to run on PR merges.

  4. Set Up Pull Request Policies: Require reviewers and status checks before merging.

  5. Save Changes.

2. GitHub – Implement Branch Merging Restrictions

Steps to Implement Branch Merging Restrictions in GitHub

  1. Navigate to Repository Settings:

    • Go to your repository on GitHub.

    • Click Settings > Branches.

  2. Add Branch Protection Rules:

    • Select the branch you want to protect (e.g., main, master).

    • Enable Branch protection.

  3. Set Protection Requirements:

    • Enable necessary restrictions:

      • Require pull requests: Ensure PRs are required before merging.

      • Require approvals: Set a minimum number of required reviewers.

      • Require status checks: Set specific checks (e.g., CI, tests) that need to pass before merging.

      • Restrict who can push: Limit who can push directly to the branch.

  4. Customize Additional Settings:

    • Enable Require review from code owners if needed.

    • Optionally enable Dismiss stale reviews and other advanced settings.

  5. Save Settings.

Example Configuration for Azure DevOps and GitHub

Azure DevOps Configuration

  1. Branch: main

  2. Protected Branch Settings:

    • Minimum reviewers: 2

    • Build Validation: Enabled (with specific build pipelines)

    • Status Checks: Required

GitHub Configuration

  1. Branch: main

  2. Branch Protection Rules:

    • Require pull requests: Enabled

    • Approvals: 2 required

    • Status checks: CI, code quality checks required

    • Restrictions: Only administrators can push directly to main

Benefits of Branch Merging Restrictions

  1. Improved Code Quality: Prevents unauthorized or poorly reviewed code from being merged into protected branches.

  2. Increased Security: Restricting who can push directly or merge ensures better control over sensitive branches.

  3. Consistency and Automation: Automating build validations, reviews, and status checks reduces manual intervention.

  4. Audit and Traceability: Easily track changes and approvals for merged code.

Related Articles


Rajnish, MCT

Leave a Reply

Your email address will not be published. Required fields are marked *


SUBSCRIBE

My newsletter for exclusive content and offers. Type email and hit Enter.

No spam ever. Unsubscribe anytime.
Read the Privacy Policy.