列出 Git 别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7066325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:34:20  来源:igfitidea点击:

List Git aliases

gitterminalalias

提问by ma11hew28

How do I print a list of my git aliases, i.e., something analogous to the bash aliascommand?

如何打印我的 git 别名列表,即类似于 bashalias命令的内容?

采纳答案by Acumenus

This answer builds upon the answerby johnny. It applies if you're not using git-aliasfrom git-extras.

这个答案建立在答案约翰尼。如果您不使用git-aliasfrom ,则适用git-extras

On Linux, run once:

在 Linux 上,运行一次:

git config --global alias.alias "! git config --get-regexp ^alias\. | sed -e s/^alias\.// -e s/\ /\ =\ /"

This will create a permanent git alias named aliaswhich gets stored in your ~/.gitconfigfile. Using it will list all of your git aliases, in nearly the same format as they are in the ~/.gitconfigfile. To use it, type:

这将创建一个名为的永久 git 别名alias,该别名将存储在您的~/.gitconfig文件中。使用它将列出所有 git 别名,格式与~/.gitconfig文件中的格式几乎相同。要使用它,请键入:

$ git alias
loga = log --graph --decorate --name-status --all
alias = ! git config --get-regexp ^alias\. | sed -e s/^alias\.// -e s/\ /\ =\ /

The following considerations apply:

以下注意事项适用:

  • To prevent the alias aliasfrom 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 aliasis but an alias and is not a feature of git.

  • To sort the listed aliases, append | sortjust before the closing double-quote. Alternatively, you can keep the aliases in ~/.gitconfigsorted.

  • 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/gitconfigfile.

  • 为防止别名alias如上所列,请| grep -v ^'alias '在结束双引号之前附加。我不建议这样做,因此用户不要忘记该命令alias只是一个别名,而不是 git 的功能。

  • 要对列出的别名进行排序,请| sort在结束双引号之前附加。或者,您可以保持别名~/.gitconfig排序。

  • 要将别名添加为系统范围的别名,请将--global(对于当前用户)替换为--system(对于所有用户)。这通常在/etc/gitconfig文件中。

回答by William Pursell

$ git config --get-regexp alias

回答by johnny

I created a git alias called (strangely enough) aliasfor exactly this purpose... handy from time to time if you use aliasing enough...

alias为了这个目的,我创建了一个名为(奇怪的是)的 git 别名......如果你使用足够的别名,不时方便......

$ git config --global alias.alias "config --get-regexp ^alias\."

$ git config --global alias.alias "config --get-regexp ^alias\."

Note, the regex makes sure the line starts with alias..

请注意,正则表达式确保该行以alias..

回答by Rimian

Another alternative (purely something I find easy to remember):

另一种选择(纯粹是我觉得很容易记住的东西):

git config --list | grep alias

git config --list | grep alias

回答by sjas

The following works under Linux, MacOSX and Windows (with msysgit).

以下适用于 Linux、MacOSX 和 Windows(使用 msysgit)。

Use git lato show aliases in .gitconfig

使用git la在 .gitconfig 中显示别名

Did I hear 'bash scripting'? ;)

我听说过“bash 脚本”吗?;)

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:

我在我的 .gitconfig 中设置了这样的命令,分隔如下TAB=TAB

[alias]
        alias1            =            foo -x -y --z-option
        alias2            =            bar -y --z-option --set-something

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.)

并简单地定义另一个别名来 grepTAB=定义的别名的一部分。(所有其他选项在其定义中的 '=' 前后都没有制表符,只有空格。)

Comments not appended to an alias also have a TAB=====appended, so they are shown after grepping.

未附加到别名的注释也有TAB=====附加,因此它们会在 grepping 后显示。

For better viewing I am piping the grep output into less, like this:

为了更好地查看,我将 grep 输出管道化为 less,如下所示:

basic version: (black/white)

基本版:(黑/白)

  #.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" 

The '\t=' part matches TAB=.

' \t=' 部分匹配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:

为了更好地了解我拥有的别名,并且由于我使用了 bash 控制台,因此我使用终端颜色对输出进行了着色:

  • all '=' are printed in red
  • all '#' are printed in green
  • 所有 '=' 都以红色打印
  • 所有“#”都以绿色打印

advanced version: (colored)

进阶版:(彩色)

       la      =       "!grep '\t=' ~/.gitconfig | sed -e 's/=/^[[0;31m=^[[0m/g' | sed -e 's/#.*/^[[0;32m&^[[0m/g' | less -R"

Basically the same as above, just sedusage is added to get the color codes into the output.

与上面基本相同,只是添加了sed用法以将颜色代码输入到输出中。

The -Rflag of lessis needed to get the colors shown in less.

需要更少-R标志来获得更少显示的颜色。

(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.

我有大约半英里的别名,根据我的需要量身定制。
此外,其中一些会随着时间的推移变化,因此毕竟手头有最新列表的最佳方法是解析 .gitconfig。

A ****short**** excerpt from my .gitconfig aliases:

我的 .gitconfig 别名中的 ****short**** 摘录:

    #  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

In my linux or mac workstations also further aliases exist in the .bashrc's, sort of like:

在我的 linux 或 mac 工作站中,.bashrc 中还存在进一步的别名,有点像:

#.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

That way no need to type git help submodule, no need for git h submodule, just gh submoduleis all that is needed to get the help. It is just some characters, but how often do you type them?

这样就不需要输入git help submodule,不需要git h submodule,只gh submodule需要获得帮助即可。这只是一些字符,但你多久输入一次?

I use all of the following, of course only with shortcuts...

我使用以下所有内容,当然只使用快捷方式......

  • add
  • commit
  • commit --amend
  • reset --hard HEAD
  • push
  • fetch
  • rebase
  • checkout
  • branch
  • show-branch (in a lot of variations)
  • shortlog
  • reflog
  • diff (in variations)
  • log (in a lot of variations)
  • status
  • show
  • notes
  • ...
  • 添加
  • 犯罪
  • 提交--修改
  • 重置 --hard HEAD
  • 拿来
  • 变基
  • 查看
  • 分支
  • show-branch(有很多变化)
  • 短日志
  • 引用日志
  • 差异(变化)
  • 日志(有很多变化)
  • 地位
  • 展示
  • 笔记
  • ...

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.

我经常不得不在没有 gui 的情况下使用 git,因为许多 git 命令在任何图形前端都没有正确实现。但是每次我使用它们时,它的方式大多是相同的。

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 them
ccm = "!git reset --soft HEAD~ && git commit"- change last commit message

在最后一段中提到的“未实现”部分:
我还没有在 GUI 中找到与此相比的内容:
sba = show-branch --color=always -a --more=10 --no-name- 显示所有本地和远程分支以及它们在其中的提交
ccm = "!git reset --soft HEAD~ && git commit"- 更改最后提交消息

From a point of view that is more simple:
How often do you type git add .or git commit -am "..."? Not counting even the rest...
Getting things to work like git aaor git ca "..."in windows,
or with bash aliases gaa/g aaor gca "..."/g ca "..."in linux and on mac's...

从更简单的角度来看:
您多久打一次字git add .git commit -am "..."?甚至不计算其余的......
让事情像git aagit ca "..."在 Windows 中一样工作,
或者在 linux 和 mac 上使用 bash 别名gaa/g aagca "..."/ g ca "..."......

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.

对于我的需要,这似乎是一件明智的事情,像这样定制 git 命令
......为了更容易使用,我只是帮助自己使用较少使用的命令,所以我不必每次都查阅手册页。命令是预定义的,查找它们尽可能容易。

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:

这是一个额外的屏幕截图,这适用于 Windows:

script working with cmd.exe

使用 cmd.exe 的脚本

BONUS: If you are on linux or mac, colorized man pages can help you quite a bit:

奖励:如果您使用的是 linux 或 mac,彩色手册页可以帮助您很多:

colorized man pages

彩色手册页

回答by mkobit

Using git varand filtering only those that start with alias:

git var仅使用和过滤以 开头的那些alias

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

回答by Anubian Noob

As other answers mentioned, git config -llists all your configuration details from your config file. Here's a partial example of that output for my configuration:

正如其他答案所提到的,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
...

So we can grep out the alias lines, using git config -l | grep alias:

因此,我们可以使用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

We can make this prettier by just cutting out the alias.part of each line, leaving us with this command:

我们可以通过cut去掉alias.每一行的部分来使它更漂亮,留下这个命令:

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

Which prints:

哪个打印:

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

Lastly, don't forget to add this as an alias:

最后,不要忘记将其添加为别名:

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

Enjoy!

享受!

回答by 176coding

for windows:

对于窗户:

git config --list | findstr "alias"

回答by VonC

I mentioned in June 2018 with "overview list - most used git commands" the Git 2.18 "use --list-cmds=alias(commit 3301d36)", that carejreports in his answer.

我在 2018 年 6 月的“概述列表 - 最常用的 git 命令”中提到了 Git 2.18“使用--list-cmds=alias提交 3301d36)”,carej他的回答中报告了这一点

 git --list-cmds=alias

In addition of that or of git config --get-regexp alias, you can combine its output with git help, whose output will change with Git 2.14.x/2.15:

除此之外git config --get-regexp alias,您还可以将其输出与 结合git help,其输出将随 Git 2.14.x/2.15 发生变化:

"git help co" now says "co is aliased to ...", not "git co is".

" git help co" 现在说 " co is aliased to ...",而不是 " git co is"。

See commit b3a8076(12 Sep 2017) by Kaartic Sivaraam (sivaraam).
(Merged by Junio C Hamano -- gitster--in commit 5079cc8, 25 Sep 2017)

请参阅Kaartic Sivaraam ( )提交的 b3a8076(2017 年 9 月 12 日)(由Junio C Hamano合并-- --提交 5079cc8,2017 年 9 月 25 日)sivaraam
gitster

help: change a message to be more precise

When the user tries to use '--help' option on an aliased command information about the alias is printed as shown below:

help: 将消息更改为更精确

当用户尝试--help在别名命令上使用“ ”选项时,有关别名的信息将打印如下:

$ git co --help
`git co' is aliased to `checkout'

This doesn't seem correct as the user has aliased only 'co' and not 'git co'.
This might even be incorrect in cases in which the user has used an alias like 'tgit'.

这似乎不正确,因为用户只使用了“ co”而不是“ git co”的别名。
在用户使用像“ tgit”这样的别名的情况下,这甚至可能是不正确的。

$ tgit co --help
`git co' is aliased to `checkout'

回答by Thomas

I use this aliasin my global ~/.gitconfig

我在我的全局中使用这个别名~/.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

to produce the following output

产生以下输出

$ git aliases
aliases   --> !git config --get-regexp ^alias\. | sed -e s/^alias.// -e s/\ /\ $(printf "3")--\>\ / | column -t -s $(printf "3") | sort -k 1
ci        --> commit -v
cim       --> commit -m
co        --> checkout
logg      --> log --graph --decorate --oneline
pl        --> pull
st        --> status
...       --> ...

(Note: This works for me in git bash on Windows. For other terminals you may need to adapt the escaping.)

注意:这在 Windows 上的 git bash 中对有用。对于其他终端,您可能需要调整转义。



Explanation

解释

  1. !git config --get-regexp ^alias\\.prints all lines from git config that start with alias.
  2. sed -e s/^alias.//removes alias.from the line
  3. sed -e s/\\ /\\ $(printf \"\\043\")--\\>\\ /replaces the first occurrence of a space with \\ $(printf \"\\043\")--\\>(which evaluates to #-->).
  4. 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.
  5. sort -k 1sorts all lines based on the value in the first column
  1. !git config --get-regexp ^alias\\.打印 git config 中以开头的所有行 alias.
  2. sed -e s/^alias.//alias.从行中删除
  3. sed -e s/\\ /\\ $(printf \"\\043\")--\\>\\ /\\ $(printf \"\\043\")--\\>(计算结果为#-->)替换第一次出现的空格。
  4. column -t -s $(printf \"\\043\")将所有行格式化为一个均匀间隔的列表。$(printf \"\\043\")计算结果为的字符#用作分隔符。
  5. sort -k 1根据第一列中的值对所有行进行排序

$(printf \"\043\")

$(printf \"\043\")

This just prints the character #(hex 043) which is used for column separation. I use this little hack so the aliasesaliasitself 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.

这只是打印#用于列分隔的字符(十六进制 043)。我使用这个小技巧,所以aliases别名本身并不包含#字符。否则它会#在打印时替换这些字符。 注意:如果您需要带有文字#符号的别名,请将其更改为另一个字符。