Prevent Pushing Remote Git Branches Using Git Hooks

May 10, 2015

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 neccessary.

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:
# hooks/update
#!/bin/sh
[ "$1" != refs/heads/master ] || {
    echo "ERROR:  you are not allowed to update master" >&2
    exit 1
}

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

$ ERROR: you are not allowed to update master

Did you find this content helpful?


Let me send you more stuff like this! Unsubscribe at any time. No spam ever. Period.


Subscribe to MarkPhelps.me

* indicates required

Discussion, links, and tweets

Mark Phelps

I'm a Software Engineer in Durham, NC. I mostly write about Go, Ruby, and some Java from time to time.