I am trying to create an alias that uses both multiple Git commands and positional parameters. There are Stackoverflow pages for each, and it would appear painfully obvious to do both, but I am having trouble.
As an example, I want to switch to branch foo and perform a status. So in my .gitconfig
, I have:
[alias]
chs = !sh -c 'git checkout $0 && git status'
which doesn't work. Whereas something like this will work.
chs = !sh -c 'git checkout $0'
echoes = !sh -c 'echo hi && echo bye'
Any insight would be appreciated.
You can define a shell function.
This will work (tested with zsh and bash):
The problem here is that the positional parameters seem to be getting sent to the shell command twice (as of git 1.9.2). To see what I mean, try this:
Then, do
git test this is my testing string
. You should observe the following output (last two lines edited here for clarity):One way to work around this would be to
This will consume the extra positional parameter as it gets applied to that last echo command and have no effect on the results.
This targets Windows batch / msysgit bash; might not work on other environments.
As Olivier Verdier and Kevin Ballard have said
[alias] chs = !git checkout $1 && git status
almost works, but gives a spurious extra insertion of the argument ...
git chs demo -> git checkout demo && git status demo
But if you add
&& :
to the end of your alias, then the spurious argument is consumed into a location tag.So
gives the correct output ...
git chs demo -> git checkout demo && git status
I was able to create multi-line and quite complex git aliases. They work fine on Windows but I assume they'd work elsewhere too, for example:
I wrote a post and have a few more examples here.