【问题标题】:Meta-alias in Git BashGit Bash 中的元别名
【发布时间】:2013-04-29 07:37:59
【问题描述】:

问题

我很想知道是否可以创建一个别名来在 Git 中创建别名,即元别名,这样就可以不用输入git config --global alias.-whatever 而是直接写git alias-whatever。

背景:

  1. 我刚开始喜欢 Bash 和 Git,最近开始为 git config --global 创建别名,例如 git cfgfrom this pagegit hist 为:

    git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short
    
  2. 最后一个例子是通过输入设置的:

    git config --global alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short'
    
  3. 使用我的 git cfg 别名,我可以将其缩短为:

    git cfg alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short'
    

但我想知道我是否可以进一步缩短它,以便 git cfg alias. 只是 git alias,部分是出于方便,部分是出于好奇,部分是出于了解更多关于 Git 和总体来说是 Bash。

尝试过的解决方案?:

  1. 我发现我可以调用git $(echo 'cfg') 并获得与git cfg 相同的结果。

  2. 所以我尝试通过输入以下内容为alias 创建别名:

    git cfg alias.alias '$(echo "config --global alias.")'
    

    希望我可以输入git alias st status 之类的内容来为git status 创建一个快速别名git st

  3. 不幸的是,这似乎不起作用,因为此时 Git 不再将 $(echo 识别为命令,即使它像 git $(echo 'cfg') 一样在别名的上下文之外工作。鉴于示例中的 st 必须从执行的回显中直接附加到 alias.,我也不确定这样的事情是否会起作用。

    李>

有人对如何实现这一点有任何建议吗?

【问题讨论】:

    标签: git bash alias


    【解决方案1】:

    来自git wiki

    Now that you know all about aliases, it might be handy to define some, using an alias:
    
        [alias]
                alias = "!sh -c '[ $# = 2 ] && git config --global alias.\"$1\" \"$2\" && exit 0 || echo \"usage: git alias <new alias> <original command>\" >&2 && exit 1' -"
    
    then define new aliases with:
    
        $ git alias new_alias original_command
    

    表达式[ $# = 2 ] 是一个shell 表达式,如果参数的数量等于2,则该表达式为真。

    结构如下:

    not <shell cmd> AND exit 0 OR <display syntax> AND exit 1
    

    哪个是shell语言,如果参数个数为2,git返回成功,则返回成功(0)。否则,打印语法错误消息,并返回失败 (1)。

    【讨论】:

    • 这不仅有效,而且解释也有帮助 :-) 谢谢!
    猜你喜欢
    • 2015-07-01
    • 2021-06-24
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多