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


当前回答

使用git var,只过滤那些以alias开头的:

git var -l | grep -e "^alias"

其他回答

有一个内置的功能…试一试

$ __git_aliases

列出所有的别名:)

$ git alias -h

'alias' is aliased to '!git config --list | grep 'alias\.' | sed 
's/alias\.\([^=]*\)=\(.*\)/\1\  => \2/' | sort'
a        => !git add . && git status
aa       => !git add . && git add -u . && git status
ac       => !git add . && git commit
acm      => !git add . && git commit -m

你可以在正则表达式^alias中使用——get-regexp,即所有以alias开头的配置

git config --get-regexp ^alias

这里是我的社区别名: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-"

享受吧!