我如何打印我的git别名列表,即,类似于bash别名命令的东西?
当前回答
另一种选择(纯粹是我觉得容易记住的东西):
Git配置——list | grep别名
其他回答
正如其他答案所提到的,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-"
享受吧!
你可以在正则表达式^alias中使用——get-regexp,即所有以alias开头的配置
git config --get-regexp ^alias
另一个git别名(称为alias)打印出git别名:添加以下到你的gitconfig [alias]部分:
[alias]
# lists aliases matching a regular expression
alias = "!f() { git config --get-regexp "^alias.${1}$" ; }; f"
示例用法,给出完整的别名(与别名完全匹配:即^foobar$),并简单地显示值:
$ git alias st
alias.st status -s
$ git alias dif
alias.dif diff
或者,给regexp,它会显示所有匹配的别名和值:
$ git alias 'dif.*'
alias.dif diff
alias.difs diff --staged
alias.difh diff HEAD
alias.difr diff @{u}
alias.difl diff --name-only
$ git alias '.*ing'
alias.incoming !git remote update -p; git log ..@{u}
alias.outgoing log @{u}..
注意:引用regexp以防止将shell扩展为glob,尽管如果/当没有文件匹配该模式时,在技术上并不需要这样做。另外:任何regexp都可以,除了不能使用^(模式开始)和$(模式结束);它们是隐含的。假设您没有使用git-extras中的git-alias。
而且,显然你们的别名也会有所不同;这些只是我配置的几个。(也许你也会发现它们很有用。)
我在全局的~/.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,所以别名本身不字面上包含#字符。否则,它将在打印时替换这些#字符。 注意:如果您需要使用#符号的别名,请将此更改为另一个字符。
我喜欢@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
推荐文章
- 如何从终端/命令行调用VS代码编辑器
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- “你有邮件”的消息在终端,os X
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支