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


当前回答

这里是我的社区别名:git aliases和git get-alias

使用git别名,您可以获得git别名的普通列表。 git get-alias <alias-name>可以得到别名内容。

git config --global alias.aliases '!f() { git config --get-regexp "^alias\." | cut -d " " -f 1 | cut -d "." -f 2 ; }; f'

git config --global alias.get-alias '!f() { git config --get-regexp "^alias\." | grep $1 ; }; f'

其他回答

正如其他答案所提到的,git config -l列出了配置文件中的所有配置细节。下面是我的配置输出的部分示例:

...
alias.force=push -f
alias.wd=diff --color-words
alias.shove=push -f
alias.gitignore=!git ls-files -i --exclude-from=.gitignore | xargs git rm --cached
alias.branches=!git remote show origin | grep \w*\s*(new^|tracked) -E
core.repositoryformatversion=0
core.filemode=false
core.bare=false
...

所以我们可以grep掉别名行,使用git config -l | grep alias:

alias.force=push -f
alias.wd=diff --color-words
alias.shove=push -f
alias.gitignore=!git ls-files -i --exclude-from=.gitignore | xargs git rm --cached
alias.branches=!git remote show origin | grep \w*\s*(new^|tracked) -E

我们可以通过删除别名来使它更漂亮。每一行的一部分,留给我们这样的命令:

git config -l | grep alias | cut -c 7-

打印:

force=push -f
wd=diff --color-words
shove=push -f
gitignore=!git ls-files -i --exclude-from=.gitignore | xargs git rm --cached
branches=!git remote show origin | grep \w*\s*(new^|tracked) -E

最后,不要忘记添加这个作为别名:

git config --global alias.la "!git config -l | grep alias | cut -c 7-"

享受吧!

我在全局的~/.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 2.18开始,你可以使用git——list-cmds=alias

使用Bash列出全局和本地Git别名。即使没有安装Git,也可以正常工作。

$ cat ~/.gitconfig .git/config 2>/dev/null | sed -n '/alias/,/\[/p' | grep -v '^\['
        co = checkout
        br = branch
        ci = commit
        st = status

2>/dev/null -隐藏配置文件不存在时的错误 Sed -n '/alias/,/\[/p' -列出alias部分的内容 Grep -v '^\[' -隐藏部分标记(以左方括号开始)

相同命令的Bash别名

$ alias gita="cat ~/.gitconfig .git/config 2>/dev/null | sed -n '/alias/,/\[/p' | grep -v '^\['"

$ gita
        co = checkout
        br = branch
        ci = commit
        st = status

这个回答是以约翰尼的回答为基础的。如果你不使用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.