Set up git alias with an input argument

2.2k views Asked by At

Git aliases can be removed with git config --global --unset alias.<badalias>.
I want to make life easier and be able to delete git aliases with: git rmalias <badalias>.
My attempt for this alias is:

git config --global alias.rmalias '!git config --global --unset alias.'

The problem I'm running into here is

error: key does not contain variable name: alias.

since the command now reads git config --global --unset alias. badalias.

How can the alias be defined such that it puts the extra parameter on the right place?

1

There are 1 answers

0
Soren Bjornstad On BEST ANSWER

The trick to more complex aliases, including ones that concatenate parameters to other text, is to create and immediately run a shell function in the alias command:

git config --global alias.rmalias '!f() { git config --global --unset "alias.$1"; }; f'

This creates a function f which uses the first parameter $1 in the Git command it runs. Then it runs the function f (which passes all the parameters given to the alias on the command line as parameters to the function).

Creating these kind of aliases on the command line requires extreme care with the spacing and quoting, so copying and pasting is a smart idea -- if you use double quotes around the whole alias your shell may do history expansion on ! and break the alias, and you leave out the spaces around the punctuation in the function definition, that can make the function definition invalid.