我如何打印我的git别名列表,即,类似于bash别名命令的东西?


当前回答

你可以创建一个别名来显示你机器上的所有git别名。运行下面的代码。

git config --global alias.alias "! git config --get-regexp ^alias\. | sed -e s/^alias\.// -e s/\ /\ =\ /"

然后,只需运行git别名。

其他回答

你可以创建一个别名来显示你机器上的所有git别名。运行下面的代码。

git config --global alias.alias "! git config --get-regexp ^alias\. | sed -e s/^alias\.// -e s/\ /\ =\ /"

然后,只需运行git别名。

我喜欢@Thomas的回答,我做了一些修改。

特点:

添加颜色 和输入参数:让用户选择命令(从git配置——get-regexp ^.) 添加过滤器

# .gitconfig

[alias]
    show-cmd = "!f() { \
        sep="㊣" ;\
        name=${1:-alias};\
        echo -n -e '\\033[48;2;255;255;01m' ;\
        echo -n -e '\\033[38;2;255;0;01m' ;\
        echo "$name"; \
        echo -n -e '\\033[m' ;\
        git config --get-regexp ^$name\\..*$2+ | \
        cut -c 1-40 | \
        sed -e s/^$name.// \
        -e s/\\ /\\ $(printf $sep)--\\>\\ / | \
        column -t -s $(printf $sep) | \
        sort -k 1 ;\
    }; f"

使用

Git显示-cmd列表别名 Git show-cmd "" st列表别名,它应该包含字符串st Git show-cmd i18n show i18n设置 Git show-cmd核心编辑器显示核心设置,它应该包含编辑器

DEMO

它在窗户上也能正常工作

解释

you can write the long script on .gitconfig use the syntax as below: [alias] your-cmd = "!f() { \ \ }; f" name=${1:-alias} same as name = $1 if $1 else -alias echo -n -e (see more echo) -n = Do not output a trailing newline. -e Enable interpretation of the following backslash-escaped '\\033[38;2;255;0;01m' (see more SGR parameters) \\033[48; : 48 means background color. \\033[38;2;255;0;0m : 38 means fore color. 255;0;0 = Red cut -c 1-40 To avoid your command is too long, so take 40 char only. sed -e 's/be_replace_string/new_string/' replace string to new string. (if you want to put the special-char(such as space, > ...) should add \\ as the prefix. column -t -s $(printf $sep) formats all lines into an evenly spaced column table. sort -k 1 sorts all lines based on the value in the first column

我在全局的~/.gitconfig中使用这个别名

# ~/.gitconfig

[alias]
    aliases = !git config --get-regexp ^alias\\. | sed -e s/^alias.// -e s/\\ /\\ $(printf \"\\043\")--\\>\\ / | column -t -s $(printf \"\\043\") | sort -k 1

产生以下输出

$ git aliases
aliases   --> !git config --get-regexp ^alias\. | sed -e s/^alias.// -e s/\ /\ $(printf "\043")--\>\ / | column -t -s $(printf "\043") | sort -k 1
ci        --> commit -v
cim       --> commit -m
co        --> checkout
logg      --> log --graph --decorate --oneline
pl        --> pull
st        --> status
...       --> ...

(注意:这适用于我在Windows上的git bash。对于其他终端,您可能需要调整转义。)


解释

!git config --get-regexp ^alias\\. prints all lines from git config that start with alias. sed -e s/^alias.// removes alias. from the line sed -e s/\\ /\\ $(printf \"\\043\")--\\>\\ / replaces the first occurrence of a space with \\ $(printf \"\\043\")--\\> (which evaluates to #-->). column -t -s $(printf \"\\043\") formats all lines into an evenly spaced column table. The character $(printf \"\\043\") which evaluates to # is used as separator. sort -k 1 sorts all lines based on the value in the first column

美元(printf \“\ 043 \”)

这只是打印用于列分隔的字符#(十六进制043)。我使用这个小hack,所以别名本身不字面上包含#字符。否则,它将在打印时替换这些#字符。 注意:如果您需要使用#符号的别名,请将此更改为另一个字符。

这个回答是以约翰尼的回答为基础的。如果你不使用git-extras中的git-alias,它也适用。

在Linux上运行一次:

git config --global alias.alias "! git config --get-regexp ^alias\. | sed -e s/^alias\.// -e s/\ /\ =\ /"

这将创建一个名为alias的永久git别名,该别名将存储在~/中。gitconfig文件。使用它将列出所有的git别名,格式几乎与~/中的相同。gitconfig文件。要使用它,输入:

$ git alias
loga = log --graph --decorate --name-status --all
alias = ! git config --get-regexp ^alias\. | sed -e s/^alias\.// -e s/\ /\ =\ /

以下考虑因素适用:

To prevent the alias alias from getting listed as above, append | grep -v ^'alias ' just before the closing double-quote. I don't recommend this so users don't forget that the the command alias is but an alias and is not a feature of git. To sort the listed aliases, append | sort just before the closing double-quote. Alternatively, you can keep the aliases in ~/.gitconfig sorted. To add the alias as a system-wide alias, replace --global (for current user) with --system (for all users). This typically goes in the /etc/gitconfig file.

我在2018年6月提到“概述列表-最常用的git命令”,git 2.18“使用——list-cmds=alias (commit 3301d36)”,carej在他的回答中报告。

 git --list-cmds=alias

除此之外,或者git config——get-regexp别名,你可以将它的输出与git帮助结合起来,它的输出将在git 2.14.x/2.15中发生变化:

“git help co”现在说“co is alias to…”,而不是“git co is”。

参见Kaartic Sivaraam提交b3a8076(2017年9月12日)。 (由Junio C Hamano—gitster—在commit 5079cc8中合并,2017年9月25日)

帮助:将信息更改为更精确的信息 当用户尝试在一个别名命令上使用'——help'选项时,关于别名的信息将打印如下所示:

$ git co --help
`git co' is aliased to `checkout'

这似乎是不正确的,因为用户只有'co'而不是'git co'的别名。 在用户使用了'tgit'这样的别名的情况下,这甚至可能是不正确的。

$ tgit co --help
`git co' is aliased to `checkout'