我该如何打印出我的git别名列表,就像bash中的alias
命令一样?
'^alias\.'
。 - Josh Leegit config -e
只编辑 .git/config
文件。但是我的大多数别名都在全局的 ~/.gitconfig
中定义。 - Boldewyngit config --global alias.aliases config --get-regexp '^alias\.'"
然后 git aliases
- Derek Greer"
符号。 - Jake Bergergit config --global alias.aliases "config --get-regexp '^alias\.'"
and then git aliases
- Ondra Žižkagit-extras
中的git-alias
,则适用本文内容。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/\ /\ =\ /
为了防止别名 alias
被列在上面,请在双引号关闭之前添加 | grep -v ^'alias '
。我不建议这样做,因为用户可能会忘记 alias
命令只是一个别名,而不是 git 的功能。
要对列出的别名进行排序,请在双引号关闭之前添加 | sort
。或者,您可以保持 ~/.gitconfig
中的别名已排序。
要将别名添加为系统范围的别名,请将 --global
(用于当前用户)替换为 --system
(适用于所有用户)。这通常放在 /etc/gitconfig
文件中。
git config --global alias.alias"!git config --get-regexp ^alias\."
。或者,通过git version
确保您拥有当前版本的git。 - Asclepiussed -e 's/\\(^alias\\)\\./\\1 = /'
。我喜欢加入一些颜色:sed -e 's/^alias\\.\\([^ ]*\\)/\\x1B[33m\\1\\x1B[0m =/'
。 - caduceus--system
而不是--global
,因为像这样的东西在系统级别上更有用,而不是用户级别上。 - ADTCalias = config --get-regexp ^alias\\.
。 - marczychgit config --global alias.alias "!git config --get-regexp ^alias\. | grep -Po '(?<=alias\.)\S*' | sort"
这个命令怎么样? - Keith Robertsongit config --list | grep alias
(注:该命令用于查看Git配置中已定义的所有别名)alias
别名,然后忘记你甚至创建了它。 - Asclepius-l
дҪңдёә--list
зҡ„еҝ«жҚ·ж–№ејҸпјҢдҪҶжҲ‘и®ӨдёәеҲӣе»әдёҖдёӘеҲ—еҮәеҲ«еҗҚзҡ„еҲ«еҗҚжҳҜдёӘеҘҪдё»ж„ҸгҖӮ - ADTCgit config --list | grep -E '^alias'
。 - MasterMind[alias]
alias1 = foo -x -y --z-option
alias2 = bar -y --z-option --set-something
#.gitconfig
[alias]
# use 'git h <command>' for help, use 'git la' to list aliases =====
h = help #... <git-command-in-question>
la = "!grep '\t=' ~/.gitconfig | less"
'\t=' 部分匹配 TAB=。
为了更好地查看我的别名,因为我使用bash控制台,我用终端颜色着色输出:
la = "!grep '\t=' ~/.gitconfig | sed -e 's/=/^[[0;31m=^[[0m/g' | sed -e 's/#.*/^[[0;32m&^[[0m/g' | less -R"
-R
标志才能在less中显示颜色。我有大约半英里长的别名,根据我的需求定制。
此外,其中一些别名会随着时间而改变,因此最好的主意是解析.gitconfig文件,以便随时掌握最新的列表。
以下是我.gitconfig文件中别名的****简短****摘录:
# choose =====
a = add #...
aa = add .
ai = add -i
# unchoose =====
rm = rm -r #... unversion and delete
rmc = rm -r --cached #... unversion, but leave in working copy
# do =====
c = commit -m #...
fc = commit -am "fastcommit"
ca = commit -am #...
mc = commit # think 'message-commit'
mca = commit -a
cam = commit --amend -C HEAD # update last commit
# undo =====
r = reset --hard HEAD
rv = revert HEAD
#.bashrc
alias g="git"
alias gh="git h"
alias gla="git la"
function gc { git c "$*" } # this is handy, just type 'gc this is my commitmessage' at prompt
这样就不需要输入 git help submodule
,也不需要 git h submodule
,只需要 gh submodule
就可以获取帮助。虽然只是一些字符,但你有多少次要输入它们呢?
当然,我使用了以下所有命令,只是用了快捷方式…
这只是我头脑中的一部分。
我经常不使用图形界面使用 git,因为很多 git 命令在任何图形界面中都没有得到很好的实现。但每次我使用它们时,大多数情况下都是以相同的方式。
在上一段中提到的“未实现”部分:sba = show-branch --color=always -a --more=10 --no-name
- 显示所有本地和远程分支以及它们内部所包含的提交ccm = "!git reset --soft HEAD~ && git commit"
- 更改最后一次提交的消息
从一个更简单的角度来看:
你有多经常输入 git add .
或者 git commit -am "..."
?甚至不算其余部分...
让事情像在Windows中使用 git aa
或者 git ca "..."
,
或者使用bash别名 gaa
/g aa
或者 gca "..."
/g ca "..."
在Linux和Mac上...
la = "!grep '\t=' ~/.gitconfig | less"
- chwi.gitconfig
中也有空格,但是一旦我用单个制表符替换它们,cmd.exe就会像预期的那样运行。 - sjas正如其他答案所提到的,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
...
因此,我们可以使用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\\.
打印所有以 alias.
开头的 git config 行。sed -e s/^alias.//
从行中删除 alias.
sed -e s/\\ /\\ $(printf \"\\043\")--\\>\\ /
将第一个空格替换为 \\ $(printf \"\\043\")--\\>
(它会被解析为 #-->
)。column -t -s $(printf \"\\043\")
将所有行格式化为均匀间隔的列表。字符 $(printf \"\\043\")
,它会被解析为 #
,用作分隔符。sort -k 1
基于第一列的值对所有行进行排序。$(printf \"\043\")
这只是打印字符 #
(十六进制 043),用于列分隔符。我使用这个小技巧,使得 aliases
alias 本身不包含字面上的 #
字符。否则,它将在打印时替换那些 #
字符。
注意:如果需要带有字面上的 #
符号的别名,请将其更改为其他字符。
我在2018年6月提到了Git 2.18中的 "overview list - most used git commands",其中提到 "使用--list-cmds=alias
(commit 3301d36)",carej在his answer中报告了这一点。
git --list-cmds=alias
除了使用git config --get-regexp alias
命令之外,您还可以将其输出与git help
命令结合使用,它的输出会随着Git 2.14.x/2.15的变化而改变:
"
git help co
"现在显示为"co is aliased to ...
",而不是"git co is
"。
请参见commit b3a8076(由Kaartic Sivaraam (sivaraam
)于2017年9月12日提交)。
(由Junio C Hamano -- gitster
--于commit 5079cc8于2017年9月25日合并)
help
:更改消息以使其更加精确当用户尝试在别名命令上使用'
--help
'选项时,将打印有关别名的信息,如下所示:
$ git co --help
`git co' is aliased to `checkout'
co
'而没有用'git co
'作为别名。tgit
'的别名时,这甚至可能是错误的。$ tgit co --help
`git co' is aliased to `checkout'
两种方法都有效
1 - 使用正则表达式获取
$ git config --get-regexp alias
2 - 使用列表
$ git config --list | grep alias