How to require confirmation for git reset --hard?

Aleksandr Panzin

How to I make sure that git asks for confirmation on any hard reset?

I have had this several times. The terminal bugged out(bash history navigation) and showed me only the last part of the command... I pressed enter and to my surprise reset all my changes.

So is there a way to make sure that under normal operation git asks for a confirmation when performing a hard reset?

kostix

Git itself doesn't implement this (for good!), and it's impossible to alias a built-in Git command, so I see two ways:

  • A custom wrapper — something like

     # cat >/usr/local/bin/mygit
     #!/bin/sh
     set -e -u
     if [ $# -ge 2 ]; then
         if [ "x$1" = "xreset" -a "x$2" = "x--hard" ]; then
             echo 'Sure?'
             read resp || exit $?
             test "x$resp" = "xyes" || exit 0
         fi
     fi
     git "$@"
     ^D
     # chmod +x $_
     $ mygit reset --hard
    
  • Shell aliases:

    cat >>~/.bashrc
    alias git /usr/local/bin/mygit
    ^D
    

    Where /usr/local/bin/mygit is taken from the previous point.

Update to incorporate the suggestion of William Pursell — a shell functon which "overrides" the external command:

# cat >>~/.bashrc
git() {
    set -e -u
    if [ $# -ge 2 ]; then
        if [ "x$1" = "xreset" -a "x$2" = "x--hard" ]; then
            echo 'Sure?'
            read resp || return $?
            test "x$resp" = "xyes" || return 0
        fi
    fi
    command git "$@"
}
^D

After that, plain git blah ... in a shell would call the wrapper function while direct /usr/bin/git would call Git directly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related