List Git aliases

后端 未结 16 1839
暖寄归人
暖寄归人 2020-12-02 03:43

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

相关标签:
16条回答
  • 2020-12-02 03:49

    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.

    0 讨论(0)
  • 2020-12-02 03:51

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

    0 讨论(0)
  • 2020-12-02 03:52
    $ git config --get-regexp alias
    
    0 讨论(0)
  • 2020-12-02 03:52

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

    git var -l | grep -e "^alias"
    
    0 讨论(0)
  • 2020-12-02 03:52

    There is a built-in function... try

    $ __git_aliases
    

    lists all the aliases :)

    0 讨论(0)
  • 2020-12-02 03:53

    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.

    0 讨论(0)
提交回复
热议问题