VonC已经在他的答案中暗示了一个外壳包装器。这是我的包装程序的Bash实现。如果将其放入您的中.bashrc
,则您的交互式外壳将支持覆盖Git内置命令以及大写别名。
# Git supports aliases defined in .gitconfig, but you cannot override Git
# builtins (e.g. "git log") by putting an executable "git-log" somewhere in the
# PATH. Also, git aliases are case-insensitive, but case can be useful to create
# a negated command (gf = grep --files-with-matches; gF = grep
# --files-without-match). As a workaround, translate "X" to "-x".
git()
{
typeset -r gitAlias="git-$1"
if 'which' "$gitAlias" >/dev/null 2>&1; then
shift
"$gitAlias" "$@"
elif [[ "$1" =~ [A-Z] ]]; then
# Translate "X" to "-x" to enable aliases with uppercase letters.
translatedAlias=$(echo "$1" | sed -e 's/[A-Z]/-\l\0/g')
shift
"$(which git)" "$translatedAlias" "$@"
else
"$(which git)" "$@"
fi
}
然后git log
,您可以通过将名为“ git-log
某处” 的脚本放入PATH中来覆盖:
#!/bin/sh
git log --abbrev-commit "$@"