List Git aliases

后端 未结 16 1840
暖寄归人
暖寄归人 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 04:02

    As other answers mentioned, git config -l lists all your configuration details from your config file. Here's a partial example of that output for my configuration:

    ...
    alias.force=push -f
    alias.wd=diff --color-words
    alias.shove=push -f
    alias.gitignore=!git ls-files -i --exclude-from=.gitignore | xargs git rm --cached
    alias.branches=!git remote show origin | grep \w*\s*(new^|tracked) -E
    core.repositoryformatversion=0
    core.filemode=false
    core.bare=false
    ...
    

    So we can grep out the alias lines, using git config -l | grep alias:

    alias.force=push -f
    alias.wd=diff --color-words
    alias.shove=push -f
    alias.gitignore=!git ls-files -i --exclude-from=.gitignore | xargs git rm --cached
    alias.branches=!git remote show origin | grep \w*\s*(new^|tracked) -E
    

    We can make this prettier by just cutting out the alias. part of each line, leaving us with this command:

    git config -l | grep alias | cut -c 7-
    

    Which prints:

    force=push -f
    wd=diff --color-words
    shove=push -f
    gitignore=!git ls-files -i --exclude-from=.gitignore | xargs git rm --cached
    branches=!git remote show origin | grep \w*\s*(new^|tracked) -E
    

    Lastly, don't forget to add this as an alias:

    git config --global alias.la "!git config -l | grep alias | cut -c 7-"
    

    Enjoy!

    0 讨论(0)
  • 2020-12-02 04:04

    Yet another git alias (called alias) that prints out git aliases: add the following to your gitconfig [alias] section:

    [alias]
        # lists aliases matching a regular expression
        alias = "!f() { git config --get-regexp "^alias.${1}$" ; }; f"
    

    Example usage, giving full alias name (matches alias name exactly: i.e., ^foobar$), and simply shows the value:

    $ git alias st
    alias.st status -s
    
    $ git alias dif
    alias.dif diff
    

    Or, give regexp, which shows all matching aliases & values:

    $ git alias 'dif.*'
    alias.dif diff
    alias.difs diff --staged
    alias.difh diff HEAD
    alias.difr diff @{u}
    alias.difl diff --name-only
    
    $ git alias '.*ing'
    alias.incoming !git remote update -p; git log ..@{u}
    alias.outgoing log @{u}..
    

    Caveats: quote the regexp to prevent shell expansion as a glob, although it's not technically necessary if/when no files match the pattern. Also: any regexp is fine, except ^ (pattern start) and $ (pattern end) can't be used; they are implied. Assumes you're not using git-alias from git-extras.

    Also, obviously your aliases will be different; these are just a few that I have configured. (Perhaps you'll find them useful, too.)

    0 讨论(0)
  • 2020-12-02 04:10

    I mentioned in June 2018 with "overview list - most used git commands" the Git 2.18 "use --list-cmds=alias (commit 3301d36)", that carej reports in his answer.

     git --list-cmds=alias
    

    In addition of that or of git config --get-regexp alias, you can combine its output with git help, whose output will change with Git 2.14.x/2.15:

    "git help co" now says "co is aliased to ...", not "git co is".

    See commit b3a8076 (12 Sep 2017) by Kaartic Sivaraam (sivaraam).
    (Merged by Junio C Hamano -- gitster -- in commit 5079cc8, 25 Sep 2017)

    help: change a message to be more precise

    When the user tries to use '--help' option on an aliased command information about the alias is printed as shown below:

    $ git co --help
    `git co' is aliased to `checkout'
    

    This doesn't seem correct as the user has aliased only 'co' and not 'git co'.
    This might even be incorrect in cases in which the user has used an alias like 'tgit'.

    $ tgit co --help
    `git co' is aliased to `checkout'
    
    0 讨论(0)
  • 2020-12-02 04:12

    Search or show all aliases

    Add to your .gitconfig under [alias]:

    aliases = !git config --list | grep ^alias\\. | cut -c 7- | grep -Ei --color \"$1\" "#"
    

    Then you can do

    • git aliases - show ALL aliases
    • git aliases commit - only aliases containing "commit"
    0 讨论(0)
提交回复
热议问题