Created
February 25, 2024 14:43
-
-
Save gbili/eb9f0d64d1ff4528033992aecf0c67db to your computer and use it in GitHub Desktop.
Prevent unintended git reset --hard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# alias this script in your shell configuration file (e.g., .bashrc, .zshrc) with something like alias git='./git-reset-safe.sh', but ensure the script is in your PATH or use the full path to the script in the alias. | |
# Check if the first argument is 'reset' and the second is '--hard' | |
if [ "$1" = "reset" ] && [ "$2" = "--hard" ]; then | |
current_dir=$(pwd) | |
echo "🚨 About to nuke changes in: $current_dir" | |
echo "Paste the directory path to prove you're serious (no take-backs!):" | |
read user_input | |
if [ "$user_input" = "$current_dir" ]; then | |
git "$@" | |
echo "💥 Boom! Changes gone. Hope you meant that!" | |
else | |
echo "🛑 Nope, that didn't match. Crisis averted!" | |
fi | |
else | |
git "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nano
, pressCtrl+O
,Enter
, and thenCtrl+X
).PATH
to use it globally (optional):.bashrc
,.zshrc
, etc.):.bashrc
to your shell’s configuration file as necessary.)Now, when you use
git reset --hard
, it will be intercepted by yourgit-reset-safe.sh
script, requiring confirmation before proceeding.