Prevent Pushing Remote Git Branches Using Git Hooks

While you usually don’t want to block your team from pushing anything to your shared repo, there are some times that this may be necessary.

For example, if you need to enact a ‘commit moratorium’ to ensure that your automated build/deploy system is not activated while you hunt down a production issue. Yes, this could be avoiding depending on your team’s git workflow, however it’s nice to know you can lock down a branch if you need to.

Here’s a snippet that will prevent updating a remote branch (which includes someone pushing to that branch) using Git Hooks.

  1. Create a file named ‘update’ on your remote (server side) Git repo in the hooks/ directory.
  2. Paste the following:

Now whenever someone tries to push to the ‘master’ branch they will receive the following:

$ ERROR: you are not allowed to update master

Mark Phelps

Senior Software Engineer @codeship. Aspiring entrepreneur.