Search code examples
gitgit-alias

Is there a robust way of checking whether a prospective Git alias name is available?


Git allows you to create your own custom Git verbs, or aliases, using

git config [--global] alias.<alias-name> "<replacement-text>"

If you're not very familiar with Git commands, though, you may choose an alias name that corresponds to an existing Git verb; as an extremely dumb example, consider

git config --global alias.log show

No panic; according to git config --help:

To avoid confusion and troubles with script usage, aliases that hide existing Git commands are ignored.

Fine, but, in that case, Git won't tell you that your new alias will be ignored.

Therefore, I'm wondering whether there is a robust (and future-proof) way of checking whether my prospective alias name correspond to an existing Git verb, before actually defining the alias.

I was thinking of searching for <alias-name> in the output of git help --all, but the latter contains more than just a list of all Git verbs, so I'm not sure how robust such an approach would be...

Any idea?


Solution

  • Sure, just run git help <command name>.

    • If the command exists, that will bring up the man page for the command in question
    • If the command is an existing alias, you'll get something along the lines of 'git l' is aliased to 'log'
    • If the command does not exist, you'll get an error message