列出 Git 别名
如何打印 git 别名列表,即类似于 bash alias
命令的内容?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何打印 git 别名列表,即类似于 bash alias
命令的内容?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(25)
您可以使用
--get -regexp
带有正则表达式^alias
,即以alias
开头的所有配置You can use
--get-regexp
with the regular expression^alias
, ie all configurations that start withalias
此答案基于 答案 >约翰尼。如果您不使用
git-alias
来自git-extras
。在 Linux 上,运行一次:
这将创建一个名为
alias
的永久 git 别名,该别名存储在您的~/.gitconfig
文件中。使用它将列出所有 git 别名,其格式与~/.gitconfig
文件中的格式几乎相同。要使用它,请键入:请注意以下事项:
要防止别名
alias
如上所述列出,请附加| grep -v ^'alias '
就在右双引号之前。我不建议这样做,这样用户就不会忘记命令alias
只是一个别名,而不是 git 的功能。要对列出的别名进行排序,请附加
|在结束双引号之前排序
。或者,您可以对~/.gitconfig
中的别名进行排序。要将别名添加为系统范围别名,请将
--global
(对于当前用户)替换为--system
(对于所有用户)。这通常位于/etc/gitconfig
文件中。This answer builds upon the answer by johnny. It applies if you're not using
git-alias
fromgit-extras
.On Linux, run once:
This will create a permanent git alias named
alias
which gets stored in your~/.gitconfig
file. Using it will list all of your git aliases, in nearly the same format as they are in the~/.gitconfig
file. To use it, type:The following considerations apply:
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 commandalias
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.我创建了一个名为(奇怪的是)
alias
的 git 别名,正是为了这个目的...如果您使用足够的别名,那么有时会很方便...$ git config --global alias.alias "config --get-regexp ^alias\."
请注意,正则表达式确保该行以
alias.
开头。I created a git alias called (strangely enough)
alias
for exactly this purpose... handy from time to time if you use aliasing enough...$ git config --global alias.alias "config --get-regexp ^alias\."
Note, the regex makes sure the line starts with
alias.
.另一种选择(纯粹是我觉得容易记住的东西):
git config --list | grep 别名
Another alternative (purely something I find easy to remember):
git config --list | grep alias
以下适用于 Linux、MacOSX 和 Windows(使用 msysgit)。
使用git la在.gitconfig中显示别名
我听到“bash脚本”了吗? ;)
关于上面评论中的“不需要”部分,我基本上为我的别名创建了一个类似于概述的手册页。为什么这么大惊小怪?这不是完全矫枉过正吗?
继续阅读...
我在 .gitconfig 中设置了这样的命令,如 TAB=TAB 分隔:
并简单地定义了另一个别名来 grep 已定义别名的 TAB= 部分。 (所有其他选项在其定义中的“=”前后没有制表符,只有空格。)
未附加到别名的注释也有一个 TAB=====,因此显示它们进行 grep 后。
为了更好地查看,我将 grep 输出传输到 less 中,如下所示:
基本版本:(黑/白)
'
\t=
' 部分匹配 TAB< kbd>=。为了更好地了解我拥有的别名,并且由于我使用 bash 控制台,我使用终端颜色对输出进行了着色:
高级版本打印:(彩色)
基本上与上面相同,只是添加了 sed 用法来将颜色代码输入到输出中。
需要 less 的
-R
标志来获取 less 中显示的颜色。(我最近发现,窗口下带有滚动条的长命令在移动设备上无法正确显示:它们的文本被切断,滚动条根本就丢失了。这可能是这里最后一个代码片段的情况,请将其保留在在旅途中查看这里的代码片段时请注意。)
为什么要发挥如此神奇的作用?
我有大约半英里的别名,根据我的需要量身定制。
其中一些随着时间的推移而变化,所以毕竟手头有最新列表的最佳想法是解析 .gitconfig。
摘自我的 .gitconfig 别名的****简短****:
在我的 linux 或 mac 工作站中,.bashrc 中还存在进一步的别名,有点像:
这样就不需要输入
git help submodule,不需要 git h submodule
,只需gh submodule
即可获得帮助。这只是一些字符,但是您多久输入一次它们?我使用以下所有内容,当然仅使用快捷方式...
这只是我的想法。
我经常不得不在没有 GUI 的情况下使用 git,因为许多 git 命令在任何图形前端中都没有正确实现。但每次我使用它们时,大多都是以同样的方式。
关于上一段提到的“未实现”部分:
我还没有在 GUI 中找到与此相比的东西:
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 上...对于我的需求来说,定制像这样的 git 命令似乎是一件明智的事情...
...为了更容易使用,我只是帮助自己使用较少使用的命令,所以我不必每次都查阅手册页。命令是预定义的,查找它们非常容易。
我的意思是,我们毕竟是程序员吗?让事情按照我们需要的方式工作是我们的工作。
这是一个额外的屏幕截图,适用于 Windows:
奖励:如果您使用的是 Linux 或 Mac ,彩色手册页可以为您提供很大帮助:
colorized man页面
The following works under Linux, MacOSX and Windows (with msysgit).
Use git la to show aliases in .gitconfig
Did I hear 'bash scripting'? ;)
About the 'not needed' part in a comment above, I basically created a man page like overview for my aliases. Why all the fuss? Isn't that complete overkill?
Read on...
I have set the commands like this in my .gitconfig, separated like TAB=TAB:
and simply defined another alias to grep the TAB= part of the defined aliases. (All other options don't have tabs before and after the '=' in their definition, just spaces.)
Comments not appended to an alias also have a TAB===== appended, so they are shown after grepping.
For better viewing I am piping the grep output into less, like this:
basic version: (black/white)
The '
\t=
' part matches TAB=.To have an even better overview of what aliases I have, and since I use the bash console, I colored the output with terminal colors:
advanced version: (colored)
Basically the same as above, just sed usage is added to get the color codes into the output.
The
-R
flag of less is needed to get the colors shown in less.(I recently found out, that long commands with a scrollbar under their window are not shown correctly on mobile devices: They text is cut off and the scrollbar is simply missing. That might be the case with the last code snippet here, keep that in mind when looking at code snippets here while on the go.)
Why get such magic to work?
I have a like half a mile of aliases, tailored to my needs.
Also some of them change over time, so after all the best idea to have an up-to-date list at hand is parsing the .gitconfig.
A ****short**** excerpt from my .gitconfig aliases:
In my linux or mac workstations also further aliases exist in the .bashrc's, sort of like:
That way no need to type
git help submodule
, no need forgit h submodule
, justgh submodule
is all that is needed to get the help. It is just some characters, but how often do you type them?I use all of the following, of course only with shortcuts...
This was just from the top of my head.
I often have to use git without a gui, since a lot of the git commands are not implemented properly in any of the graphical frontends. But everytime I put them to use, it is mostly in the same manner.
On the 'not implemented' part mentioned in the last paragraph:
I have yet to find something that compares to this in a GUI:
sba = show-branch --color=always -a --more=10 --no-name
- show all local and remote branches as well as the commits they have within themccm = "!git reset --soft HEAD~ && git commit"
- change last commit messageFrom a point of view that is more simple:
How often do you type
git add .
orgit commit -am "..."
? Not counting even the rest...Getting things to work like
git aa
orgit ca "..."
in windows,or with bash aliases
gaa
/g aa
orgca "..."
/g ca "..."
in linux and on mac's...For my needs it seemed a smart thing to do, to tailor git commands like this...
... and for easier use I just helped myself for lesser used commands, so i dont have to consult the man pages everytime. Commands are predefined and looking them up is as easy as possible.
I mean, we are programmers after all? Getting things to work like we need them is our job.
Here is an additional screenshot, this works in Windows:
BONUS: If you are on linux or mac, colorized man pages can help you quite a bit:
colorized man pages
正如其他答案所提到的, git config -l 列出了配置文件中的所有配置详细信息。这是我的配置的部分输出示例:
因此我们可以使用 git config -l | grep 出别名行。 grep alias:
我们可以通过
剪切
每行的alias.
部分来使这个更漂亮,留下这个命令:它打印:
最后,不要忘记将其添加为别名:
享受!
As other answers mentioned,
git config -l
lists all your configuration details from your config file. Here's a partial example of that output for my configuration:So we can grep out the alias lines, using
git config -l | grep alias
:We can make this prettier by just
cut
ting out thealias.
part of each line, leaving us with this command:Which prints:
Lastly, don't forget to add this as an alias:
Enjoy!
两者都很好用
1 - 使用 Get Regex
2 - 使用列表
Both Works Well
1 - Using Get Regex
2 - Using list
使用
git var
并仅过滤以别名:
Using
git var
and filtering only those that start withalias
:我在全局
~/.gitconfig
中使用此别名来生成以下输出
(注意:这对我来说有用>Windows 上的 git bash。对于其他终端,您可能需要调整转义。)
解释
!git config --get-regexp ^alias\\.
打印所有行从 git config 开始alias.
sed -es/^alias.//
从行sed -es/\\ /\\ 中删除
将第一次出现的空格替换为alias.
$(printf \"\\043\")--\\>\\ /\\ $(printf \"\\043\")-- \\>
(计算结果为#-->
)。column -t -s $(printf \"\\043\")
将所有行格式化为均匀间隔的列表。计算结果为#
的字符$(printf \"\\043\")
用作分隔符。sort -k 1
根据第一列中的值对所有行进行排序$(printf \"\043\")
这仅打印字符
#(十六进制 043),用于列分离。我使用这个小技巧,因此
aliases
alias 本身并不真正包含#
字符。否则,它会在打印时替换那些#
字符。注意:如果您需要带有文字
#
符号的别名,请将其更改为其他字符。I use this alias in my global
~/.gitconfig
to produce the following output
(Note: This works for me in git bash on Windows. For other terminals you may need to adapt the escaping.)
Explanation
!git config --get-regexp ^alias\\.
prints all lines from git config that start withalias.
sed -e s/^alias.//
removesalias.
from the linesed -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\")
This just prints the character
#
(hex 043) which is used for column separation. I use this little hack so thealiases
alias itself does not literally contain the#
character. Otherwise it would replace those#
characters when printing.Note: Change this to another character if you need aliases with literal
#
signs.我在 2018 年 6 月的“概述列表 - 最常用的 git 命令”中提到了 Git 2.18“使用
--list -cmds=别名
(提交 3301d36)",即 carej 报告他的答案。除此之外,您还可以将其输出与
git help
,其输出将随 Git 2.14.x/2.15 改变:请参阅 提交 b3a8076(2017 年 9 月 12 日),作者:Kaartic西瓦拉姆 (
sivaraam
)。(由 Junio C Hamano --
gitster
-- 合并于 提交5079cc8,2017 年 9 月 25 日)I mentioned in June 2018 with "overview list - most used git commands" the Git 2.18 "use
--list-cmds=alias
(commit 3301d36)", that carej reports in his answer.In addition of that or of
git config --get-regexp alias
, you can combine its output withgit help
, whose output will change with Git 2.14.x/2.15:See commit b3a8076 (12 Sep 2017) by Kaartic Sivaraam (
sivaraam
).(Merged by Junio C Hamano --
gitster
-- in commit 5079cc8, 25 Sep 2017)只是添加这个,因为它太简单了,而且我在之前的答案中没有看到它(如果我错过了,很抱歉)。
您必须滚动到底部(使用
>
正如 ma11hew28 指出的那样)才能看到列表,例如:如果您甚至忘记了这个开关,一个简单的
git help
将帮助您记住:Just adding this because it's so simple and I didn't see it in previous answers (sorry if I missed it).
You'll have to scroll to the bottom (use
>
as ma11hew28 pointed out) to see the list, e.g.:If you forget even this switch, a simple
git help
will help you remember:对于窗户:
for windows:
搜索或显示所有别名
添加到
[alias]
下的.gitconfig
中:然后您可以执行
git aliases
- 显示所有别名git别名提交
- 仅包含“commit”的别名Search or show all aliases
Add to your
.gitconfig
under[alias]
:Then you can do
git aliases
- show ALL aliasesgit aliases commit
- only aliases containing "commit"从 git 2.18 开始,您可以使用 git --list-cmds=alias
As of git 2.18 you can use
git --list-cmds=alias
这个简单的解决方案对我来说效果很好,
git config --global alias.aliases "config --get-regexp '^alias\.'"
git aliases
以列出我们所有的其他< /strong> 别名this simple solution worked well for me
git config --global alias.aliases "config --get-regexp '^alias\.'"
git aliases
to list all of our other aliases我喜欢@Thomas 的答案,我做了一些修改。
功能:
git config --get-regexp ^.
)USAGE
git show-cmd
list aliasst
git show-cmd i18n
showi18n
设置git show-cmd 核心编辑器
显示core
设置,并且应包含editor
DEMO
它在 Windows 上也运行良好
说明
you可以写长脚本
.gitconfig
使用如下语法:<前><代码>[别名]
你的cmd = "!f() { \
\
}; ”
name=${1:-alias}
与name = $1 相同 if $1 else -别名
echo -n -e
(请参阅更多echo)'\\033[38;2;255;0;01m'
的解释(请参阅更多SGR 参数)\\033[48;
:48表示背景颜色。\\033[38;2;255;0;0m
:38 表示前景色。 255;0;0 = 红色cut -c 1-40
为了避免命令太长,所以只取 40 个字符。sed -e 's/be_replace_string/new_string/'
将字符串替换为新字符串。 (如果你想放置特殊字符(例如空格
,>
...)应添加\\
作为前缀。column -t -s $(printf $sep)
将所有行格式化为均匀间隔的列表。sort -k 1
根据第一列中的值对所有行进行排序I like @Thomas's answer, and I do some modifications.
features:
git config --get-regexp ^.
)USAGE
git show-cmd
list aliasgit show-cmd "" st
list alias, and it should contain the stringst
git show-cmd i18n
showi18n
settinggit show-cmd core editor
showcore
setting, and it should containeditor
DEMO
It's working fine on windows too
Explanation
you can write the long script on
.gitconfig
use the syntax as below:name=${1:-alias}
same asname = $1 if $1 else -alias
echo -n -e
(see more echo)'\\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 = Redcut -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 asspace
,>
...) 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您可以创建一个别名来显示您计算机上的所有 git 别名。运行下面的代码。
然后,只需运行 git alias 即可。
You can create an alias to show all git alias on your machine. Run below code.
then, simply run
git alias
.有一个内置函数...尝试
列出所有别名:)
There is a built-in function... try
lists all the aliases :)
打开.gitconfig文件(C:\Users\user.gitconfig)--Windows
下[alias]复制并复制粘贴以下代码
alias = !git config --list | grep ^别名\\. |切-c 7- | grep -Ei --color \"$1\" "#"
在终端
git alias
-- 列出所有别名在终端
git alias commit
-- 列出与提交相关的所有别名< /strong>获取所有别名的列表,而无需记住代码:)
Open .gitconfig file (C:\Users\user.gitconfig) --Windows
Under [alias] copy & paste the below code
alias = !git config --list | grep ^alias\\. | cut -c 7- | grep -Ei --color \"$1\" "#"
In terminal
git alias
-- Lists all aliasesIn terminal
git alias commit
-- Lists all aliases related to commitGet list of all aliases without remembering the code :)
如果您知道别名的名称,则可以使用
--help
选项来描述它。例如:If you know the name of the alias, you can use the
--help
option to describe it. For example:还有一个
git
别名(称为alias
),用于打印 git 别名:将以下内容添加到您的 gitconfig[alias]
部分:示例用法,给出完整别名(与别名完全匹配:即
^foobar$
),并简单显示值:或者,给出 regexp,它显示所有匹配的别名 &值:
注意事项:引用正则表达式以防止 shell 扩展为 glob,尽管如果/当没有文件与模式匹配时,这在技术上不是必需的。另外:任何正则表达式都可以,但不能使用
^
(模式开始)和$
(模式结束);它们是隐含的。假设您没有使用git-extras
中的git-alias
。另外,显然你的别名会有所不同;这些只是我配置的一些。 (也许您也会发现它们很有用。)
Yet another
git
alias (calledalias
) that prints out git aliases: add the following to your gitconfig[alias]
section:Example usage, giving full alias name (matches alias name exactly: i.e.,
^foobar$
), and simply shows the value:Or, give regexp, which shows all matching aliases & values:
Caveats: quote the regexp to prevent shell expansion as a glob, although it's not technically necessary if/when no files match the pattern. Also: any regexp is fine, except
^
(pattern start) and$
(pattern end) can't be used; they are implied. Assumes you're not usinggit-alias
fromgit-extras
.Also, obviously your aliases will be different; these are just a few that I have configured. (Perhaps you'll find them useful, too.)
这是我为社区提供的别名:
git aliases
和git get-alias
通过 您可以获得别名内容。
git aliases
,您可以获得 git 别名的简单列表。使用 git get-alias
Here my aliases for the community:
git aliases
andgit get-alias
With
git aliases
you get the plain list of your git aliases.With
git get-alias <alias-name>
you get the alias content.使用 Bash 列出全局和本地 Git 别名。即使未安装 Git,这也将起作用。
2>/dev/null
- 在配置文件不存在的情况下隐藏错误sed -n '/alias/,/\[/p'
- 列出别名部分的内容grep -v '^\[' - 隐藏节标记(它们以左方括号开头)
同一命令的 Bash 别名
List global and local Git aliases using Bash. This will work even if Git is not installed.
2>/dev/null
- hides errors in case config files do not existsed -n '/alias/,/\[/p'
- lists contents of alias section(s)grep -v '^\['
- hides section markers (they begin with the left square bracket)Bash alias for the same command
我发现这对于列出 shell 中设置的所有 Git 别名(包括来自 Oh My Zsh Git 插件的别名)很有帮助:
I find this helpful for listing all Git aliases set in the shell (including those from the Oh My Zsh Git plugin):