Skip to content

Instantly share code, notes, and snippets.

@mcgwiz
Last active October 31, 2020 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcgwiz/dc30e23293ba7a13a88e00cdc509ea2a to your computer and use it in GitHub Desktop.
Save mcgwiz/dc30e23293ba7a13a88e00cdc509ea2a to your computer and use it in GitHub Desktop.
pre-push hook to block force-pushing to certain branches on Git for Windows MSYS bash
#!/bin/sh
# based on https://gist.github.com/stefansundin/d465f1e331fc5c632088#file-pre-push
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
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."
echo
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment