How To Protect The Master Branch On GitHub

Owanate Amachree
4 min readJul 31, 2020

--

A step-by-step guide on how to protect the master and staging branch during first time setup to prevent unwanted changes to the official branch.

Photo by Erica Nilsson on Unsplash

Requirements

  • Github Account
  • Knowledge of Git and GitHub

Note: This guide was created using only the GitHub interface, No usage Git from the Command-Line.

Getting Started

1. Create a GitHub Repository

To create a GitHub repository, navigate to your GitHub Profile, read this article on how to set up a Github account if you don’t have one.

On your GitHub profile, click on the new button to create a new GitHub repository

Input a name for the Repository, and select “initialize project with a readme”

Click on “Create Repository” button

2. Create A Staging Branch

To create multiple branches using GitHub, navigate to Master as shown in the image below, Click on the button and type in “Staging” in the input box to create a Staging Branch.

Then, click enter and it takes you directly to the Staging branch

Follow this pattern to create as many branches you may need, try creating a Development branch to familiarize yourself with the steps.

3. Protect The Master Branch

To protect the master branch, navigate to Settings.

Click on Branches by the left-hand corner

Click Add rule

On clicking Add rule, enter the name of Branch which is “master”

Select “Require Pull request reviews before merging” this will force a code review before changes are merged to the master branch.

Click on the Create button when done

Enter a password to make a sensitive change.

NB: This step is optional, so don’t fret when it doesn’t require a password.

So now we have set up a rule on the Master branch

Same steps can be applied to the staging branch. I will allow you to do that as an exercise😉

4. Commit to Master

Now let’s see what it will look like when we try to commit to the master branch.

Navigate to code then to the master branch, by clicking on master

Let’s make a change to the README.md file directly on GitHub

Click on the pen icon to edit the file

Insert extra details concerning your repository.

Click on “Create a new branch for this commit and start a pull request”, this will create a new branch off of master and then create a pull request to merge those changes into the master branch.

Then, click on the Propose Changes button

This opens up a Pull Requests page with a message. Click on Create pull request button

Now we see we need a review before we can merge this change.

Since I’m an administrator and have no other person working on this codebase with me I can’t ask anyone to review it, so for now, I still have the ability to merge pull request, but if I were a junior developer or someone new working on this codebase who is working with me, I would want to review these changes before allowing such changes to be made within our official branches.

Click on the Merge pull request button

Click on Use your administrator privileges to merge this pull request, and then click the Confirm merge button.

Congratulations on setting up protection for master and staging branch, now no one on your team can directly push changes to the master branch unless approved!!

Additional Resources

Thanks for reading!!!

--

--

Owanate Amachree
Owanate Amachree

Written by Owanate Amachree

Content Developer | Technical Writer | Editor| Blockchain Enthusiast | Ask me anything about Content Strategy, Technical Documentation and Developer Portals.

No responses yet