Skip to content

Instantly share code, notes, and snippets.

@skipcloud
Last active January 27, 2020 14:03
Show Gist options
  • Select an option

  • Save skipcloud/f37063913cf357681a7374f7d66307f3 to your computer and use it in GitHub Desktop.

Select an option

Save skipcloud/f37063913cf357681a7374f7d66307f3 to your computer and use it in GitHub Desktop.
Don't push to Master

Just had a fun moment where I didn't realise I was on the master branch of repo and was able to force push after a rebase (a dance I'm so used to during development I do it almost without thinking). If you're like me and don't want that to happen to you you can set up a safe guard to stop you ever being able to push to master. What you need in your life is a simple git hook. Here are the steps to create your own if you want

Tell git where to find your templates for new repos:

> git config --global init.templatedir $HOME/.git_template

Next, create that directory with a hooks directory inside it

> mkdir -p $HOME/.git_template/hooks

Next cd into that hooks directory and create a pre-push file, be sure to make it executable

> cd $HOME/.git_template/hooks
> touch pre-push
> chmod ug+x pre-push

open up the pre-push file in your favourite editor and add this code

#!/bin/sh
branch=$(git rev-parse --abbrev-ref HEAD)
if [ branch = "master" ]; then
  echo "Can't push to master" >&2
  exit 1
fi

Now anytime you initialise a repo git will place this in the hooks folder of the repo, and it will run this file before it pushes some changes. You can add this to repos you have already got on your local but running git init inside them but beware it will overwrite any hooks already set up in that repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment