How do I print a list of my git aliases, i.e., something analogous to the bash alias command?
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.)
!git config --get-regexp ^alias\\. prints all lines from git config that start with alias.sed -e s/^alias.// removes alias. 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 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.
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 --get-regexp alias
Using git var and filtering only those that start with alias:
git var -l | grep -e "^alias"
There is a built-in function... try
$ __git_aliases
lists all the aliases :)
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.