列出Git别名

本文翻译自:List Git aliases

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


#1楼

参考:https://stackoom.com/question/TeGz/列出Git别名


#2楼

The following works under Linux, MacOSX and Windows (with msysgit). 以下适用于Linux,MacOSX和Windows(使用msysgit)。

Use git la to 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. 并简单地定义另一个别名来grep TAB =定义的别名的一部分。 (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. 未附加到别名的注释也附加了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 sed usage is added to get the color codes into the output. 基本上与上面相同,只是添加了sed用法以将颜色代码输入到输出中。

The -R flag of less is 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别名的****短****摘录:

    #  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 submodule is all that is needed to get the help. 那样就不需要输入git help submodule ,不需要git h submodule ,只需要gh submodule就可以得到帮助。 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 commit --amend
  • reset --hard HEAD 重置 - 硬头
  • push
  • fetch
  • rebase 变基
  • checkout 查看
  • branch
  • show-branch (in a lot of variations) show-branch(有很多变化)
  • shortlog 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. 我经常不得不使用没有gui的git,因为很多git命令在任何图形前端都没有正确实现。 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: 我还没有在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 sba = show-branch --color=always -a --more=10 --no-name - 显示所有本地和远程分支以及它们在其中的提交
ccm = "!git reset --soft HEAD~ && git commit" - change last commit message ccm = "!git reset --soft HEAD~ && git commit" - 更改上次提交消息

From a point of view that is more simple: 从更简单的角度来看:
How often do you type git add . 你多久输入一次git add . or git commit -am "..." ? 或者git commit -am "..." Not counting even the rest... 不计算其余的......
Getting things to work like git aa or git ca "..." in windows, 在Windows中像git aagit ca "..."一样工作
or with bash aliases gaa / g aa or gca "..." / g ca "..." in linux and on mac's... 或者使用bash别名gaa / g aagca "..." / g ca "..."在linux和Mac上...

For my needs it seemed a smart thing to do, to tailor git commands like this... 对于我的需求,这似乎是一件明智的事情,要定制像这样的git命令......
... 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: 这是一个额外的截图,这适用于Windows:

使用cmd.exe的脚本

BONUS: If you are on linux or mac, colorized man pages can help you quite a bit: 奖励:如果您使用的是Linux或Mac,彩色手册页可以帮助您:

colorized man pages 彩色手册页


#3楼

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

git config --list | grep alias


#4楼

This answer builds upon the answer by johnny . 这个答案建立在答案约翰尼 It applies if you're not using git-alias from git-extras . 如果您没有使用git-extras git-alias ,它适用。

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 alias which gets stored in your ~/.gitconfig file. 这将创建一个名为alias的永久git别名,该alias存储在~/.gitconfig文件中。 Using it will list all of your git aliases, in nearly the same format as they are in the ~/.gitconfig file. 使用它将列出所有git别名,其格式与~/.gitconfig文件中的格式几乎相同。 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 optional considerations apply: 以下可选注意事项适用:

  • To prevent the alias alias from getting listed as above, append | grep -v ^'alias ' 要防止别名alias如上所列,请附加| grep -v ^'alias ' | grep -v ^'alias ' just before the closing double-quote. 在结束双引号之前的| grep -v ^'alias ' I don't recommend this for Linux users so that they don't forget that the the command alias is but an alias and is not a feature of git. 我不建议Linux用户这样做,以便他们不要忘记命令alias只是别名而不是git的功能。

  • To sort the listed aliases, append | sort 要对列出的别名进行排序,请追加| sort | sort just before the closing double-quote. 在结束双引号之前| sort Alternatively, you can keep the aliases in ~/.gitconfig sorted. 或者,您可以将~/.gitconfig的别名排序。

  • To add the alias as a system-wide alias, replace --global (for current user) with --system (for all users). 要将别名添加为系统范围的别名,请将--global (对于当前用户)替换为--system (对于所有用户)。 This typically goes in the /etc/gitconfig file. 这通常位于/etc/gitconfig文件中。


#5楼

If you know the name of the alias, you can use the --help option to describe it. 如果您知道别名的名称,则可以使用--help选项来描述它。 For example: 例如:

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

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

#6楼

There is a built-in function... try 有一个内置函数...试试

$ __git_aliases

lists all the aliases :) 列出所有别名:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值