Skip to content

Instantly share code, notes, and snippets.

@TSMMark
Created September 9, 2016 18:58
Show Gist options
  • Save TSMMark/6c9d648372c5cc8bdfee845812c00068 to your computer and use it in GitHub Desktop.
Save TSMMark/6c9d648372c5cc8bdfee845812c00068 to your computer and use it in GitHub Desktop.
Prevent pushing to master in your git project!
# PUT THIS IN .git/hooks/pre-push IN YOUR PROJECT.
#!/bin/bash
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
push_command=$(ps -ocommand= -p $PPID)
if [ $current_branch = $protected_branch ] || [[ $push_command =~ $protected_branch ]]
then
read -p "You're about to push master, is that 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment