Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@joaomarcos96
Last active February 8, 2021 13:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaomarcos96/66fbc9a7705e248e9c00496e1b94eab9 to your computer and use it in GitHub Desktop.
Save joaomarcos96/66fbc9a7705e248e9c00496e1b94eab9 to your computer and use it in GitHub Desktop.
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

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