最近 3 次,我在使用git
. 我git reset --hard
在我的主目录存储库上运行了两次。我第一次在我的 shell 中粗指了一个反向历史搜索(根本不是要运行它),第二次我在错误的终端窗口中(旨在重置不同的存储库)。另一个错误是git push --mirror ssh://remote-machine/
从错误的存储库运行。
git help config
通知我“为了避免脚本使用的混淆和麻烦,隐藏现有 git 命令的别名将被忽略。”,所以我的 .git/config 别名
[alias]
reset = "!echo no"
push = "!echo wrong repo"
Run Code Online (Sandbox Code Playgroud)
被忽略。有没有办法简单地做到这一点?我可能会alias git=wrapped-git
在我的 shell 中编写某种包装脚本,但我希望有一种更简单的方法来做到这一点。
更新:使用以下内容,基于grawity 的答案,但利用 git 的内置配置系统。这避免了对临时文件进行 grep'ing,并且它允许“级联”(~/.gitconfig 全局禁用“reset”,但每个 repo .git/config 启用它)。在我的 .zshrc 中:
git () {
local disabled=$(command git config --bool disabled.$1 2>/dev/null)
if ${disabled:-false} ; then
echo "The $1 command is intentionally disabled" >&2
return 1
fi
command git "$@"
}
Run Code Online (Sandbox Code Playgroud)
不完全是一个包装脚本 - 您可以创建一个 shell 函数:
git() {
local gitdir=$(git rev-parse --git-dir 2>/dev/null)
if [[ $gitdir && -f $gitdir/disabled-commands ]]; then
# "disabled-commands" should contain "push", "reset", etc
if grep -Fwqse "$1" "$gitdir/disabled-commands"; then
echo "You have disabled this command." >&2
return 1
else
command git "$@"
fi
else
command git "$@"
fi
}
Run Code Online (Sandbox Code Playgroud)
没有比这更简单的方法了。
编辑:添加-e
到 grep 中:如果没有它,grep 会干扰诸如 之类的调用git --version
,它变成了grep -Fwqs --version
,并且还具有制表符补全功能。