gitgithubbranchaccess-controlpull-request

Prevent pushing to master on GitHub?


GitHub allows you to configure your repository so that users can't force push to master, but is there a way to prevent pushing to master entirely? I'm hoping to make it so that the only way of adding to commits to master is through the GitHub pull request UI.


Solution

  • Since the original question / answer, Github has added a new option for this to the restricted branches UI which allows you to set this up.

    Require pull request reviews before merging When enabled, all commits must be made to a non-protected branch and submitted via a pull request with the required number of approving reviews and no changes requested before it can be merged into a branch that matches this rule.

    To find it go to Settings > Branches > Branch Protection Rules and click 'Add Rule'. Github UI after following instructions Then, enter the name of the branch you want to protect.

    Slightly below that, click the checkbox to "Require a pull request before merging"
    Screenshot of Github UI "Require a pull request before merging" option

    By default, this only stops people who are not moderators. There is also another checkbox later down for ensuring that even moderators cannot merge, "Do not allow bypassing the above settings"
    Screenshot of Github UI "Do not allow bypassing the above settings" option