列出 Git 别名

发布于 2024-11-29 19:03:20 字数 56 浏览 1 评论 0 原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(25

迷你仙 2024-12-06 19:03:20

您可以使用 --get -regexp 带有正则表达式^alias,即以alias开头的所有配置

git config --get-regexp ^alias

You can use --get-regexp with the regular expression ^alias, ie all configurations that start with alias

git config --get-regexp ^alias
尴尬癌患者 2024-12-06 19:03:20

此答案基于 答案 >约翰尼。如果您不使用 git-alias 来自 git-extras

在 Linux 上,运行一次:

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 的功能。

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

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

This answer builds upon the answer by johnny. It applies if you're not using git-alias from git-extras.

On Linux, run once:

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

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:

$ 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 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 command alias 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.

A君 2024-12-06 19:03:20

我创建了一个名为(奇怪的是) 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..

新雨望断虹 2024-12-06 19:03:20

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

git config --list | grep 别名

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

git config --list | grep alias

兲鉂ぱ嘚淚 2024-12-06 19:03:20

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

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

我听到“bash脚本”了吗? ;)

关于上面评论中的“不需要”部分,我基本上为我的别名创建了一个类似于概述的手册页。为什么这么大惊小怪?这不是完全矫枉过正吗?

继续阅读...

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

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

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

未附加到别名的注释也有一个 TAB=====,因此显示它们进行 grep 后。

为了更好地查看,我将 grep 输出传输到 less 中,如下所示:

基本版本:(黑/白)

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

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

  • 所有“=”均以红色打印,
  • 所有“#”均以绿色

高级版本打印:(彩色)

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

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

需要 less-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

在我的 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

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

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

  • add
  • commit
  • commit --amend
  • reset --hard HEAD
  • Push
  • fetch
  • rebase
  • checkoutbranch
  • show- branch
  • (有很多变体)
  • shortlog
  • reflog
  • diff(有变体)
  • log(有很多变化)
  • 状态
  • 显示
  • 注释
  • ...

这只是我的想法。

我经常不得不在没有 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 aagit ca "..." 一样工作,
或使用 bash 别名 gaa/g aagca "..."/g ca "..." 在 Linux 和 Mac 上...

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

我的意思是,我们毕竟是程序员吗?让事情按照我们需要的方式工作是我们的工作。

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

scriptworking with cmd.exe

奖励:如果您使用的是 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:

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

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)

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

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:

  • 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 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:

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

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

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

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

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 aa or git ca "..." in windows,
or with bash aliases gaa/g aa or gca "..."/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:

script working with cmd.exe

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

colorized man pages

安人多梦 2024-12-06 19:03:20

正如其他答案所提到的, 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 出别名行。 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

我们可以通过剪切每行的alias.部分来使这个更漂亮,留下这个命令:

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

享受!

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:

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

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:

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!

祁梦 2024-12-06 19:03:20

两者都很好用

1 - 使用 Get Regex

$ git config --get-regexp alias

2 - 使用列表

$ git config --list | grep alias

Both Works Well

1 - Using Get Regex

$ git config --get-regexp alias

2 - Using list

$ git config --list | grep alias
娇女薄笑 2024-12-06 19:03:20

使用 git var 并仅过滤以 别名

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

Using git var and filtering only those that start with alias:

git var -l | grep -e "^alias"
奢欲 2024-12-06 19:03:20

我在全局 ~/.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。对于其他终端,您可能需要调整转义。


解释

  1. !git config --get-regexp ^alias\\. 打印所有行从 git config 开始alias.
  2. sed -es/^alias.// 从行
  3. sed -es/\\ /\\ 中删除 alias. $(printf \"\\043\")--\\>\\ / 将第一次出现的空格替换为 \\ $(printf \"\\043\")-- \\>(计算结果为#-->)。
  4. column -t -s $(printf \"\\043\") 将所有行格式化为均匀间隔的列表。计算结果为 # 的字符 $(printf \"\\043\") 用作分隔符。
  5. sort -k 1 根据第一列中的值对所有行进行排序

$(printf \"\043\")

这仅打印字符 #(十六进制 043),用于列分离。我使用这个小技巧,因此 aliases alias 本身并不真正包含 # 字符。否则,它会在打印时替换那些 # 字符。
注意:如果您需要带有文字 # 符号的别名,请将其更改为其他字符。

I use this alias in my global ~/.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 "\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
...       --> ...

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


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 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 the aliases 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.

莳間冲淡了誓言ζ 2024-12-06 19:03:20

我在 2018 年 6 月的“概述列表 - 最常用的 git 命令”中提到了 Git 2.18“使用 --list -cmds=别名 (提交 3301d36)",即 carej 报告他的答案

 git --list-cmds=alias

除此之外,您还可以将其输出与 git help,其输出将随 Git 2.14.x/2.15 改变:

git help co”现在显示“co 的别名为 ...”,而不是“git co is”。

请参阅 提交 b3a8076(2017 年 9 月 12 日),作者:Kaartic西瓦拉姆 (sivaraam)
(由 Junio C Hamano -- gitster -- 合并于 提交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'

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.

 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 help co" now says "co is aliased to ...", not "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)

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:

$ 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'.

$ tgit co --help
`git co' is aliased to `checkout'
流年已逝 2024-12-06 19:03:20

只是添加这个,因为它太简单了,而且我在之前的答案中没有看到它(如果我错过了,很抱歉)。

git help -a

您必须滚动到底部(使用 > 正如 ma11hew28 指出的那样)才能看到列表,例如:

Command aliases
   restore-deleted      !git restore $(git ls-files -d)

如果您甚至忘记了这个开关,一个简单的 git help 将帮助您记住:

'git help -a' 和 'git help -g' 列出可用的子命令和一些
概念指南。请参阅“git help”或“git help”
了解特定子命令或概念。

Just adding this because it's so simple and I didn't see it in previous answers (sorry if I missed it).

git help -a

You'll have to scroll to the bottom (use > as ma11hew28 pointed out) to see the list, e.g.:

Command aliases
   restore-deleted      !git restore $(git ls-files -d)

If you forget even this switch, a simple git help will help you remember:

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help ' or 'git help ' to
read about a specific subcommand or concept.

我还不会笑 2024-12-06 19:03:20

对于窗户:

git config --list | findstr "alias"

for windows:

git config --list | findstr "alias"
征﹌骨岁月お 2024-12-06 19:03:20

搜索或显示所有别名

添加到 [alias] 下的 .gitconfig 中:

aliases = !git config --list | grep ^alias\\. | cut -c 7- | grep -Ei --color \"$1\" "#"

然后您可以执行

  • git aliases - 显示所有别名
  • git别名提交 - 仅包含“commit”的别名

Search or show all aliases

Add to your .gitconfig under [alias]:

aliases = !git config --list | grep ^alias\\. | cut -c 7- | grep -Ei --color \"$1\" "#"

Then you can do

  • git aliases - show ALL aliases
  • git aliases commit - only aliases containing "commit"
梅倚清风 2024-12-06 19:03:20

从 git 2.18 开始,您可以使用 git --list-cmds=alias

As of git 2.18 you can use git --list-cmds=alias

夕色琉璃 2024-12-06 19:03:20

这个简单的解决方案对我来说效果很好,

  1. 创建一个用于列出别名的别名:)
    git config --global alias.aliases "config --get-regexp '^alias\.'"
  2. 执行它 git aliases 以列出我们所有的其他< /strong> 别名

this simple solution worked well for me

  1. create an alias for listing aliases :)
    git config --global alias.aliases "config --get-regexp '^alias\.'"
  2. execute it git aliases to list all of our other aliases
浅暮の光 2024-12-06 19:03:20

我喜欢@Thomas 的答案,我做了一些修改。

功能:

  • 添加颜色
  • 和输入参数:让用户选择命令(来自 git config --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"

USAGE

  1. git show-cmd list alias
  2. < code>git show-cmd "" st 列出别名,它应该包含字符串 st
  3. git show-cmd i18n show i18n 设置
  4. git show-cmd 核心编辑器显示 core 设置,并且应包含 editor

DEMO

在此处输入图像描述

它在 Windows 上也运行良好

说明

  • you可以写长脚本.gitconfig 使用如下语法:

    <前><代码>[别名]
    你的cmd = "!f() { \
    \
    }; ”

  • name=${1:-alias}name = $1 相同 if $1 else -别名

  • echo -n -e (请参阅更多echo

    • -n = 不输出尾随换行符。
    • -e 启用对以下反斜杠转义的解释
  • '\\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:

  • add color
  • and input parameter: to let the user choose command (from git config --get-regexp ^.)
  • add filter
# .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"

USAGE

  1. git show-cmd list alias
  2. git show-cmd "" st list alias, and it should contain the string st
  3. git show-cmd i18n show i18n setting
  4. git show-cmd core editor show core setting, and it should contain editor

DEMO

enter image description here

It's working fine on windows too

Explanation

  • 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

怪我入戏太深 2024-12-06 19:03:20

您可以创建一个别名来显示您计算机上的所有 git 别名。运行下面的代码。

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

然后,只需运行 git alias 即可。

You can create an alias to show all git alias on your machine. Run below code.

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

then, simply run git alias.

屌丝范 2024-12-06 19:03:20

有一个内置函数...尝试

$ __git_aliases

列出所有别名:)

There is a built-in function... try

$ __git_aliases

lists all the aliases :)

流年里的时光 2024-12-06 19:03:20
  1. 打开.gitconfig文件(C:\Users\user.gitconfig)--Windows

  2. [alias]复制并复制粘贴以下代码

    alias = !git config --list | grep ^别名\\. |切-c 7- | grep -Ei --color \"$1\" "#"

  3. 在终端 git alias -- 列出所有别名

  4. 在终端 git alias commit -- 列出与提交相关的所有别名< /strong>


  5. 获取所有别名的列表,而无需记住代码:)

  1. Open .gitconfig file (C:\Users\user.gitconfig) --Windows

  2. Under [alias] copy & paste the below code

    alias = !git config --list | grep ^alias\\. | cut -c 7- | grep -Ei --color \"$1\" "#"

  3. In terminal git alias -- Lists all aliases

  4. In terminal git alias commit -- Lists all aliases related to commit

  5. Get list of all aliases without remembering the code :)

家住魔仙堡 2024-12-06 19:03:20

如果您知道别名的名称,则可以使用 --help 选项来描述它。例如:

$ git sa --help
`git sa' is aliased to `stash'

$ git a --help
`git a' is aliased to `add'

If you know the name of the alias, you can use the --help option to describe it. For example:

$ git sa --help
`git sa' is aliased to `stash'

$ git a --help
`git a' is aliased to `add'
攀登最高峰 2024-12-06 19:03:20

还有一个 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}..

注意事项:引用正则表达式以防止 shell 扩展为 glob,尽管如果/当没有文件与模式匹配时,这在技术上不是必需的。另外:任何正则表达式都可以,但不能使用 ^ (模式开始)和 $ (模式结束);它们是隐含的。假设您没有使用 git-extras 中的 git-alias

另外,显然你的别名会有所不同;这些只是我配置的一些。 (也许您也会发现它们很有用。)

Yet another git alias (called alias) that prints out git aliases: add the following to your gitconfig [alias] section:

[alias]
    # lists aliases matching a regular expression
    alias = "!f() { git config --get-regexp "^alias.${1}$" ; }; f"

Example usage, giving full alias name (matches alias name exactly: i.e., ^foobar$), and simply shows the value:

$ git alias st
alias.st status -s

$ git alias dif
alias.dif diff

Or, give regexp, which shows all matching aliases & values:

$ 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}..

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 using git-alias from git-extras.

Also, obviously your aliases will be different; these are just a few that I have configured. (Perhaps you'll find them useful, too.)

蒲公英的约定 2024-12-06 19:03:20

这是我为社区提供的别名:git aliasesgit get-alias

通过 git aliases,您可以获得 git 别名的简单列表。
使用 git get-alias 您可以获得别名内容。

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'

Here my aliases for the community: git aliases and git 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.

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'
花落人断肠 2024-12-06 19:03:20

使用 Bash 列出全局和本地 Git 别名。即使未安装 Git,这也将起作用。

$ cat ~/.gitconfig .git/config 2>/dev/null | sed -n '/alias/,/\[/p' | grep -v '^\['
        co = checkout
        br = branch
        ci = commit
        st = status

2>/dev/null - 在配置文件不存在的情况下隐藏错误
sed -n '/alias/,/\[/p' - 列出别名部分的内容
grep -v '^\[' - 隐藏节标记(它们以左方括号开头)

同一命令的 Bash 别名

$ alias gita="cat ~/.gitconfig .git/config 2>/dev/null | sed -n '/alias/,/\[/p' | grep -v '^\['"

$ gita
        co = checkout
        br = branch
        ci = commit
        st = status

List global and local Git aliases using Bash. This will work even if Git is not installed.

$ cat ~/.gitconfig .git/config 2>/dev/null | sed -n '/alias/,/\[/p' | grep -v '^\['
        co = checkout
        br = branch
        ci = commit
        st = status

2>/dev/null - hides errors in case config files do not exist
sed -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

$ alias gita="cat ~/.gitconfig .git/config 2>/dev/null | sed -n '/alias/,/\[/p' | grep -v '^\['"

$ gita
        co = checkout
        br = branch
        ci = commit
        st = status
半衬遮猫 2024-12-06 19:03:20

我发现这对于列出 shell 中设置的所有 Git 别名(包括来自 Oh My Zsh Git 插件的别名)很有帮助:

$ alias | grep "git "

I find this helpful for listing all Git aliases set in the shell (including those from the Oh My Zsh Git plugin):

$ alias | grep "git "
第七度阳光i 2024-12-06 19:03:20
$ 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
$ 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文