List Git aliases

后端 未结 16 2013
暖寄归人
暖寄归人 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.

提交回复
热议问题