Skip to content

Instantly share code, notes, and snippets.

@joaomarcos96
Last active Feb 8, 2021
Embed
What would you like to do?
git pre-push hook to confirm push force or delete on any branch
#!/bin/sh
current_branch=$(git rev-parse --abbrev-ref HEAD)
push_command=$(ps -ocommand= -p $PPID)
is_destructive='force|delete|\-f'
confirm_destructive_action(){
read -p 'Are you sure you want to push to "'$current_branch'" ? (y/n): ' -n 1 -r < /dev/tty
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
exit 0
fi
echo 'Push aborted.'
exit 1
}
if [[ $push_command =~ $is_destructive ]]; then
confirm_destructive_action
fi
unset confirm_destructive_action
exit 0
@joaomarcos96
Copy link
Author

joaomarcos96 commented Jan 15, 2021

Install pre-push hook:

  1. Copy the script content to <your_repo>/.git/hooks/pre-push
  2. Make it executable: chmod +x <your_repo>/.git/hooks/pre-push

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment