Skip to content

Instantly share code, notes, and snippets.

@penguwin

penguwin/pre-push

Created Oct 19, 2020
Embed
What would you like to do?
A git hook to prevent force pushing on master
#!/bin/bash
# Per-repo installation
# 1. Place this file in `.git/hooks/pre-push`
# 2. `chmod +x` it
clippy='
_________________________________________
/ It looks like youre trying to force \
| push on master... |
\ Are you sure you want to continue? /
-----------------------------------------
\
\
__
/ \
| |
@ @
| |
|| |/
|| ||
|\_/|
\___/
'
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
is_destructive='force|delete|\-f'
push_command=$(ps -ocommand= -p $PPID)
if [[ $push_command =~ $is_destructive ]] && [ $protected_branch = $current_branch ]
then
echo "${clippy}"
read -p '[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
You can’t perform that action at this time.