git1 Min Read

How to Protect Git branch and Enforce Restrictions

Gorav Singal

August 03, 2018

TL;DR

Configure Git branch protection rules to restrict direct commits to important branches like master and stage, enforcing code review and merge best practices.

How to Protect Git branch and Enforce Restrictions

In previous article: Effective Git Branching Strategy, I discussed Git branching strategy and how you can follow best practices to manage your code and builds.

In this article, I will show how you can protect your git branches and restrict them by implementing the best practices.

Create Stage Branch

Open your Master branch. On left hand drop down, you can see only Master branch. Start typing “stage”, and it will give you an option to create that branch. See image below:

Create Stage branch from Master

Protect Your branches

Here, we are going to restrict our two branches: master and stage for two things:

  1. Any code merge will require code review
  2. Only specific person can merge the code
  • Click on Settings tab on your repository.
  • Click on Branches section from left side menu.

Protect branches

  • Click on Branch Protection Rules, and click on Master:

Branch protection rules

Check on Two options: “Protect this branch”, and “Restrict who can push to this branch”, and do appropriate settings: Branch protection rules

Click on Save changes.

Similarly do this for Stage branch.

Share

Related Posts

An Effective GIT Branching Strategy

An Effective GIT Branching Strategy

Its essential to prepare a git branching strategy. This helps greatly in…

A Practical Guide in understanding Git Branch and Conflict resolution during merge

A Practical Guide in understanding Git Branch and Conflict resolution during merge

Introduction In this guide, We will learn about branching, handling conflict…

A Practical Guide on Understanding Git Best Practices

A Practical Guide on Understanding Git Best Practices

Introduction In this post, we will learn about some of Best practices while…

A Practical Guide on how to work with Git Basic Commands and workflows

A Practical Guide on how to work with Git Basic Commands and workflows

Introduction In this guide, we will see git basic commands, and fundamentals of…

A Practical Guide on how to work with Git log command and history

A Practical Guide on how to work with Git log command and history

Introduction In this post, we will see ways to look at git history logs. For…

A Practical Guide for better understanding Git Diff

A Practical Guide for better understanding Git Diff

Introduction In this guide, We will get basic understanding of various options…

Latest Posts

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI video generation went from “cool demo” to “usable in production” in 2024-202…

AI Models in 2025 — Cost, Capabilities, and Which One to Use

AI Models in 2025 — Cost, Capabilities, and Which One to Use

Choosing the right AI model is one of the most impactful decisions you’ll make…

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

Generating one image with AI costs between $0.002 and $0.12. That might sound…

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

Two years ago, AI coding meant one thing: GitHub Copilot autocompleting your…

AI Agents Demystified — It's Just Automation With a Better Brain

AI Agents Demystified — It's Just Automation With a Better Brain

Let’s cut through the noise. If you read Twitter or LinkedIn, you’d think “AI…

Supply Chain Security — Protecting Your Software Pipeline

Supply Chain Security — Protecting Your Software Pipeline

In 2024, a single malicious contributor nearly compromised every Linux system on…