Reputation: 3194
Question
I'm curious to know if one can create an alias for making aliases in Git, i.e. a meta-alias, so that instead of typing git config --global alias.
-whatever one can just write git alias
-whatever.
Background:
I'm just warming to Bash and Git, and have recently started creating aliases like git cfg
for git config --global
and, from this page, git hist
for:
git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short
This last example is set by typing:
git config --global alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short'
With my git cfg
alias I can shorten that to:
git cfg alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short'
But I'm trying to figure out if I can shorten it further still so that git cfg alias.
is just git alias
, partly out of convenience, partly out of curiosity, partly out of a desire to learn more about Git and Bash in general.
Attempted solution?:
I have found I can call git $(echo 'cfg')
and get the same results as git cfg
.
So I tried to create an alias for alias
by entering this:
git cfg alias.alias '$(echo "config --global alias.")'
in the hope that I could type something like git alias st status
to create a quick alias git st
for git status
.
Unfortunately, this does not seem to work because at this point Git no longer recognises $(echo
as a command, even though it worked outside the context of the alias, as with git $(echo 'cfg')
. I'm also not sure if something like this would work given that the st
in the example would have to be appended directly to the alias.
from the executed echo.
Does anyone have any suggestions as to how to make this possible?
Upvotes: 3
Views: 248
Reputation: 238296
From the git wiki:
Now that you know all about aliases, it might be handy to define some, using an alias:
[alias]
alias = "!sh -c '[ $# = 2 ] && git config --global alias.\"$1\" \"$2\" && exit 0 || echo \"usage: git alias <new alias> <original command>\" >&2 && exit 1' -"
then define new aliases with:
$ git alias new_alias original_command
The expression [ $# = 2 ]
is a shell expression that is true if the number of arguments equals two.
The structure looks like:
not <shell cmd> AND exit 0 OR <display syntax> AND exit 1
Which is shell language for, if the number of arguments is two, and git returns success, then return success (0). Else, print a syntax error message, and return failure (1).
Upvotes: 2