问题
我很想知道是否可以创建一个别名来在 Git 中创建别名,即元别名,而不是输入 git config --global alias.
-随便写一个git alias
-随便。
背景:
我刚刚开始接触 Bash 和 Git,并且最近开始创建别名,例如
git cfg
对于git config --global
和 from this page ,git hist
对于:git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short
最后一个示例是通过键入设置的:
git config --global alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short'
和我的
git cfg
别名我可以将其缩短为:git cfg alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short'
但我想弄清楚是否可以进一步缩短它,以便 git cfg alias.
只是git alias
,部分是出于方便,部分是出于好奇,部分是出于总体上了解 Git 和 Bash 的愿望。
尝试的解决方案?
我发现我可以调用
git $(echo 'cfg')
并得到与git cfg
相同的结果.所以我尝试为
alias
创建一个别名通过输入:git cfg alias.alias '$(echo "config --global alias.")'
希望我能输入类似
git alias st status
的内容创建快速别名git st
对于git status
.不幸的是,这似乎不起作用,因为此时 Git 不再识别
$(echo
作为命令,即使它在别名的上下文之外工作,如git $(echo 'cfg')
.考虑到st
,我也不确定这样的事情是否会有效在示例中必须直接附加到alias.
来自执行的回显。
有没有人对如何实现这一目标有任何建议?
最佳答案
来自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 表达式,如果参数的个数等于二则为真。
结构如下:
not <shell cmd> AND exit 0 OR <display syntax> AND exit 1
这是shell语言,如果参数个数为2,git返回成功,则返回成功(0)。否则,打印语法错误消息,并返回失败 (1)。
关于git - Git Bash 中的元别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16383661/