DEV Community

loading...

How to avoid pushing to branches that are not protected (that should be)

mickmister profile image Michael Kochell Updated on ・2 min read

Git hooks are used to run some arbitrary code before and after different git operations. The ones that begin with pre- are used before the operation, and can be used to bail out of the operation. post- hooks are run after an operation, but do not have built-in functionality revert the operation itself.

We're going to use pre-push and pre-commit hooks to make sure we never accidentally push to an un-protected master branch. It is only going to affect our own local copies of the repositories. You will need git 2.9+ (June 2016). The script we will create will ask you if you want to commit/push to master, so you will still be able to if that's your intention.

Make a place to house your global hooks.

mkdir -p ~/.githooks # this name/location doesn't matter

Create a file called pre-push in this directory.

#!/bin/bash

protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

if [ $protected_branch = $current_branch ]
then
    read -p "You're about to push master, is this what you intended? [y|n] " -n 1 -r < /dev/tty
    echo
    if echo $REPLY | grep -E '^[Yy]$' > /dev/null
    then
        exit 0 # push will execute
    fi
    exit 1 # push will not execute
else
    exit 0 # push will execute
fi

We should also make a pre-commit hook. To do this, copy the pre-push file to a new file pre-commit, and change to the prompt to say "commit" instead of "push". pre-commit is arguably more important and sort of renders pre-push useless. You can never be too careful though.

Set the files to be executable:

chmod a+x ~/.githooks/pre-push
chmod a+x ~/.githooks/pre-commit

Set your global hooks folder to be recognized by git:

git config --global core.hooksPath ~/.githooks

Now if you try to commit or push to master on any repo on your machine, the hook will ask you to make sure you want to do that.

If you want to apply this on a per project basis, then you will need to add the pre-push and pre-commit files to the project's .git/hooks folder instead of a global hooks folder.

Discussion (0)

pic
Editor guide