Prevent pushing to master on GitHub?

joshlf picture joshlf · Sep 11, 2017 · Viewed 53.4k times · Source

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.

Answer

Ignacio Arces picture Ignacio Arces · Sep 18, 2019

The current accepted answer is actually correct but if you are an organization owner or have admin privileges, which is the case if you created the repo, you can still push to protected branches. From Github documentation at https://help.github.com/en/articles/about-branch-restrictions:

Organization owners and people with admin permissions for a repository are always able to push to a protected branch.

For any other type of collaborator, git push will fail.

If you really want to disable pushing qt all, then you have to setup that locally either by configuring an invalid pushRemote for a branch as mentioned before:

git config branch.master.pushRemote no_push

or you can setup a pre-push hook as shown here: https://gist.github.com/vlucas/8009a5edadf8d0ff7430