Last Updated: February 25, 2016
·
800
· antonioribeiro

Bash protection against "git reset --hard HEAD"

If you ever execute git reset --hard HEAD on a unstaged and uncommited repository, be prepared to loose everything up to your last commit. If you code has been staged, git reflog or git fsck may save you, but, still, you're about to have a lot of trouble to find your files.

This is a an alias script, intended to ask you a question before deleting your work. Create a file and source this on bash:

git() {
    if [[ "$1" = "reset" ]] && [ "$2" = "--hard" ] && [ "$3" = "HEAD" ] ; then
        echo "are you being a moron again? (yes/no)"
        read i
        if [ "$i" != "no" ]; then
            echo "you're safe, reset not executed"
            return 0
        fi
    fi

    command git "$@"
}

2 Responses
Add your response

Got any background articles on why it's bad to use git reset --hard HEAD ?

over 1 year ago ·

Protip edited to give some background info on why is this so scaring...

over 1 year ago ·