Skip to content

Instantly share code, notes, and snippets.

Last active October 31, 2020 00:03
What would you like to do?
pre-push hook to block force-pushing to certain branches on Git for Windows MSYS bash
# based on
BRANCH=`git rev-parse --abbrev-ref HEAD`
# customization for cygwin/msys
PID=`ps | grep ' /cmd/git$' | sort -n | tail -n 1 | tr -s ' ' | cut -d ' ' -f 2`
PUSH_COMMAND=`cat /proc/$PID/cmdline | tr '\000' ' '`
if [[ "$BRANCH" =~ main|master && "$PUSH_COMMAND" =~ force|delete|-f ]]; then
echo "Prevented force-push to $BRANCH. This is a very dangerous command."
echo "If you really want to do this, use --no-verify to bypass this pre-push hook."
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment