2013-05-05 86 views
2

问题元别名Git中的Bash

我很好奇,想知道是否可以在Git的制作别名,即元别名创建一个别名,以便不用打字git config --global alias. -whatever一个能只要写git alias - 无论如何。

背景:

  1. 我只升温到Bash和Git的,并且最近已经开始创建别名像git cfggit config --global和,from 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。希望我能像键入一些git alias st status创建一个快捷别名git stgit status

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

  2. 所以,我想通过输入此创建别名为alias

  3. 不幸的是,这似乎并没有在这一点上的Git不再承认$(echo的命令,因为工作,即使它的工作别名的背景之外,与git $(echo 'cfg')。我也不确定是否工作,因为示例中的st必须从执行的回显中直接附加到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 ]是壳表达式,如果参数的数目等于二这是真的。

的结构是这样的:

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

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

+0

这不仅工作,但解释也有帮助:-)谢谢! – guypursey 2013-05-06 09:26:45