7

As a GitHub administrator, I would like to lock a particular branch in GitHub for all users. For e.g. if I do not want anyone to push to Master/Production or a Project branch, how can I do that.

Instead of using any client side hooks, is there a way to lock a branch on GitHub server directly ? Are there any third party tools/api's/scripts which can help achieve this ?

Saurabh Porwal
  • 81
  • 1
  • 1
  • 4
  • As of September 2015, you can now protect branches directly in Github: https://github.com/blog/2051-protected-branches-and-required-status-checks – Matt Grande Jul 28 '16 at 15:48

2 Answers2

6

@Saurabh, I have done a similar thing according to your requirement on GitHub:

  • Navigate to Settings
  • Navigate to Branches
  • Tap on Add Rule near "Branch protection rules"
  • Tick the Require pull request reviews before merging checkbox

These steps apply a lock on, for example to master, so that no collaborators can push code to this branch. Code only be merged using pull requests.

Link to documentation

Screenshots:

enter image description here

enter image description here

enter image description here

Note: Protected branches are available to Pro, Team, and Enterprise users

note pro feature only

Umutambyi Gad
  • 3,970
  • 3
  • 15
  • 34
CrazyPro007
  • 850
  • 7
  • 13
2

The easiest solution is to have that branch in its own repo (for which no collaborators) are declared.

Otherwise, GitHub doesn't provide any native "branch protection" feature, as mentioned in "How to protect “master” in github?"

You could setup a webhook which on a push event can refuse the push if the branch has a given name.

An example would be terite/pull-to-master which protects master:

if (json.ref != 'refs/heads/master')
  return cb([200, 'Skipping, not master']);

This is not a client-side hook, but it does require a client to listen to the JSON payload of the push event in order to react to it.

Community
  • 1
  • 1
VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755