How to require confirmation for git reset --hard?

前端 未结 1 1110
Happy的楠姐
Happy的楠姐 2021-01-12 13:55

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 on

1条回答
  •  既然无缘
    2021-01-12 14:05

    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.

    0 讨论(0)
提交回复
热议问题