Skip to content

Instantly share code, notes, and snippets.

@mcgwiz

mcgwiz/pre-push.git-msys

Last active Oct 31, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.