git|August 03, 2018|1 min read

How to Protect Git branch and Enforce Restrictions

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.

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 on how to to create your own git command alias

A Practical Guide on how to to create your own git command alias

Introduction In this guide, We will learn on how to create some handy command…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…