Branch protection, pull requests and code owners

AZ-400 · Design and implement a source control strategy · Updated

Branch policies are how a team makes the main branch safe. The exam asks which control, in which product, achieves a given rule.

Azure Repos branch policies

Set policies on a branch such as main in Project settings or the branch menu. The main ones:

  • Require a minimum number of reviewers, with options to block self-approval and to reset votes when new changes are pushed.
  • Check for linked work items, which forces traceability.
  • Check for comment resolution, so open comments block completion.
  • Limit merge types: basic merge, squash, rebase and fast-forward, or semi-linear merge.
  • Build validation: runs a pipeline on every pull request and blocks completion until it succeeds.
  • Automatically included reviewers: path-based reviewers, for example a security group for /infra.
  • Status checks: external services can post a pass or fail.

Policies apply to a branch or a branch pattern. Admins can bypass them only if they hold the Bypass policies permission.

GitHub branch protection

GitHub uses a branch protection rule (or the newer rulesets) on a branch name pattern. Typical settings:

SettingEffect
Require a pull request before mergingNo direct pushes
Require approvalsNumber of reviewers
Require review from Code OwnersMatches the CODEOWNERS file
Require status checksWorkflow or app checks must pass
Require signed commitsOnly verified signatures
Require linear historyBlocks merge commits

CODEOWNERS

A CODEOWNERS file lives in the repository root, .github/ or docs/. Each line maps a path to users or teams:

/src/auth/   @contoso/security-team
*.tf         @contoso/platform

On its own the file only requests reviews. It becomes mandatory when the branch protection rule requires code owner review. Protect the branch you deploy from (main), not the feature branches.

Choosing a branching model

  • Trunk-based: short-lived branches, merge to main often, feature flags for unfinished work. Best fit for continuous delivery.
  • GitFlow: long-lived develop and release branches. Heavier, used when releases are scheduled.
  • Release flow (Microsoft): topic branches into main, release branches cut from main for hotfixes.

Exam traps

  • A CODEOWNERS file alone does not block merges; the protection rule must require code owner review.
  • Putting the protection rule on feature branches instead of main.
  • Using a release gate when the question wants a pre-merge check. Build validation is pre-merge; gates are in release.
  • Assuming squash keeps every commit. It produces one.
  • Forgetting that Azure Repos uses policies and GitHub uses protection rules, and the names are not interchangeable.

Practice questions

Choose an answer, then review the explanation to check your understanding.

1. Every change to main in a GitHub repository must be reviewed by the security team before merge. What enforces this?

Show answer

Answer: A.CODEOWNERS names the reviewers, and the branch protection rule on main makes their approval mandatory.

2. In Azure Repos, a build must succeed before a pull request can complete. What do you configure?

Show answer

Answer: B.Build validation is a branch policy that queues a pipeline for each pull request and blocks completion until it passes.

3. Your team wants a linear history on main with one commit per pull request. Which merge type do you allow?

Show answer

Answer: B.Squash merge collapses the branch into one commit. Rebase and fast-forward also keep history linear but retain each commit.

4. You need to stop developers from approving their own pull request in Azure Repos. Which policy setting do you use?

Show answer

Answer: B.The minimum reviewers policy has the option Allow requestors to approve their own changes. Turn it off.

AZ-400 course outline

All 7 lessons · Course overview
  1. Azure Boards, work item links and flow metricsConfigure processes and communications Free
  2. Branch protection, pull requests and code ownersDesign and implement a source control strategy Free
  3. Git history cleanup, large files and repository sizeDesign and implement a source control strategy Full access
  4. YAML pipelines, agents, triggers and templatesDesign and implement build and release pipelines Full access
  5. Deployment strategies, environments and approvalsDesign and implement build and release pipelines Full access
  6. Secrets, managed identities and code scanningDevelop a security and compliance plan Full access
  7. Application Insights, alerts and KQLImplement an instrumentation strategy Full access