如何要求确认git reset --hard?

Ale*_*zin 5 git bash

如何确保git在任何硬重置时要求确认?

我已经好几次了.终端出了问题(bash历史导航),只向我展示了命令的最后一部分...我按了回车键,让我惊讶地重置了所有的更改.

那么有没有办法确保在正常操作下git在执行硬重置时要求确认?

kos*_*tix 5

Git本身没有实现这个(好!),并且不可能为内置的Git命令添加别名,所以我看到两种方式:

  • 自定义包装器 - 类似于

     # 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
    
    Run Code Online (Sandbox Code Playgroud)
  • Shell别名:

    cat >>~/.bashrc
    alias git /usr/local/bin/mygit
    ^D
    
    Run Code Online (Sandbox Code Playgroud)

    /usr/local/bin/mygit从以前的点取.

更新以结合William Pursell的建议 - 一个"覆盖"外部命令的shell功能:

# 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
Run Code Online (Sandbox Code Playgroud)

之后,git blah ...shell中的plain 将调用包装函数,而direct直接/usr/bin/git调用Git.