定义具有相同名称的 git 别名以隐藏原始命令

发布于 2024-11-05 21:13:38 字数 224 浏览 1 评论 0原文

我试图使用与现有命令相同的别名作为别名,以便别名隐藏原始命令(防止我从工作树中删除文件)。

[alias]
   rm = rm --cached
   diff = diff --color

不幸的是,这不起作用。有人知道解决方法吗? 谢谢。

编辑 设置 color.diff = true 会默认提供彩色输出。

I'm trying to use to use the same name for an alias as the existing command, so that the alias shadows the original command (preventing me from deleting files off the working tree).

[alias]
   rm = rm --cached
   diff = diff --color

Unfortunatly this is not working. Does anyone know a workaround?
Thanks.

Edit
Setting color.diff = true gives colored output as default.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

闻呓 2024-11-12 21:13:38

对于像 rm --cached 这样没有可配置选项的命令,最好的办法是创建一个不同名称的别名。例如:

[alias]
        rmc = rm --cached

您可能已经明白了这一点,但 Git 别名无法隐藏现有的 Git 命令。来自 git-config 人页面

为了避免脚本使用方面的混乱和麻烦,隐藏现有 git 命令的别名将被忽略。

For commands like rm --cached that don't have configurable options, your best bet is to just make an alias named differently. For example:

[alias]
        rmc = rm --cached

You may have already figured this out, but Git aliases cannot shadow existing Git commands. From the git-config man page:

To avoid confusion and troubles with script usage, aliases that hide existing git commands are ignored.

心意如水 2024-11-12 21:13:38

作为解决方法,您可以在 Bash 中定义别名来获得您想要的结果。这是我刚刚因为我的小烦恼而敲出来的东西 - 默认情况下“git add”并不冗长。 (并且没有它的配置设置)。

将其放入您的 ~/.bash_profile~/.bash_rc

function do_git {
  cmd=$1
  shift
  extra=""
  if [ "$cmd" == "add" ]; then
    extra="-v"
  elif [ "$cmd" == "rm" ]; then
    extra="--cached"
  fi
  git="$(which git)"
  ex="$git $cmd $extra $@"
  ${ex}
}
alias  git='do_git'

然后像平常一样调用它:

$ git add .
add 'foo'

As a workaround, you can define aliases in Bash to get the result you want. Here's something I just knocked up for a pet peeve of mine - that 'git add' is not verbose by default. (And there's no config setting for it).

Put this in your ~/.bash_profile or ~/.bash_rc

function do_git {
  cmd=$1
  shift
  extra=""
  if [ "$cmd" == "add" ]; then
    extra="-v"
  elif [ "$cmd" == "rm" ]; then
    extra="--cached"
  fi
  git="$(which git)"
  ex="$git $cmd $extra $@"
  ${ex}
}
alias  git='do_git'

Then just call it like normal:

$ git add .
add 'foo'
请别遗忘我 2024-11-12 21:13:38

Steve Bennett 的答案适用于简单的命令,但当您引用如下所示的参数时会中断:

$ git commit -m "foo bar" --allow-empty
error: pathspec 'bar' did not match any file(s) known to git.

将完整的参数列表保留为数组似乎可以工作:

function do_git {
  cmd=$1
  shift
  myArgs=( "$@" )

  if [ "$cmd" == "add" ]; then
    myArgs=( "-v" "${myArgs[@]}" )
  elif [ "$cmd" == "rm" ]; then
    myArgs=( "--cached" "${myArgs[@]}" )
  fi
  myArgs=( "$cmd" "${myArgs[@]}" )
  $(which git) "${myArgs[@]}"
}
alias  git='do_git'

现在命令成功:

$ git commit -m "foo bar" --allow-empty
/usr/bin/git commit -m foo bar --allow-empty
[master 699af14] foo bar

Steve Bennett's answer works for simple commands, but breaks when you have quoted arguments like the following:

$ git commit -m "foo bar" --allow-empty
error: pathspec 'bar' did not match any file(s) known to git.

Preserving the full list of arguments as an array seems to work:

function do_git {
  cmd=$1
  shift
  myArgs=( "$@" )

  if [ "$cmd" == "add" ]; then
    myArgs=( "-v" "${myArgs[@]}" )
  elif [ "$cmd" == "rm" ]; then
    myArgs=( "--cached" "${myArgs[@]}" )
  fi
  myArgs=( "$cmd" "${myArgs[@]}" )
  $(which git) "${myArgs[@]}"
}
alias  git='do_git'

Now the command succeeds:

$ git commit -m "foo bar" --allow-empty
/usr/bin/git commit -m foo bar --allow-empty
[master 699af14] foo bar
南七夏 2024-11-12 21:13:38

这是 Steve Bennett 翻译为 oh-my-zsh 的答案

function do_git {
  cmd=$1
  shift
  extra=""
  if [ "$cmd" '==' "add" ]; then
    extra="-v"
  elif [ "$cmd" '==' "rm" ]; then
    extra="--cached"
  fi
  "`whence -p git`" "$cmd" "$extra" "$@"
}
alias  git='do_git'

等号需要用引号引起来。
这不起作用,因为它只是返回“git”是一个别名。

This is the answer of Steve Bennett translated for oh-my-zsh

function do_git {
  cmd=$1
  shift
  extra=""
  if [ "$cmd" '==' "add" ]; then
    extra="-v"
  elif [ "$cmd" '==' "rm" ]; then
    extra="--cached"
  fi
  "`whence -p git`" "$cmd" "$extra" "$@"
}
alias  git='do_git'

The equals sign needs to be wrapped in quotes.
And which doesn't work as it just return that 'git' is an alias.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文