git - Git Bash 中的元别名

标签 git bash alias

问题

我很想知道是否可以创建一个别名来在 Git 中创建别名,即元别名,而不是输入 git config --global alias. -随便写一个git alias -随便。

背景:

  1. 我刚刚开始接触 Bash 和 Git,并且最近开始创建别名,例如 git cfg对于 git config --globalfrom this page , git 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 st对于 git status .

  3. 不幸的是,这似乎不起作用,因为此时 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/

相关文章:

linux - 使用 bash 脚本在 plink 中缓存 RSA key 指纹

bash - grep 查找具有特定名称的目录中所有文件中的字符串

javascript - 在 javascript 中创建 "new function"别名的最短方法

node.js - 无法使用任何环境变量

git - CentOS6 上的 ssh 不再适用于 GitHub

linux - kubectl 没有显示环境变量

apache - 使用 Apache 修复别名目录上的 403 Forbidden

git - 使用 git 以非交互方式编辑旧提交

git - 从一个 repo git clone 并将 origin 设置为另一个是 "safe"吗?

wamp - 本地主机文件夹的别名