List Git aliases

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


Solution 1:

$ git config --get-regexp alias

Solution 2:

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.

Solution 3:

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

Solution 4:

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

git config --list | grep alias

Solution 5:

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