How to git alias this git complex git command with single and double quotes inside

178 Views Asked by At

Im trying to create a git alias to this command:

git branch -vv | grep ': gone]'|  grep -v "\*" | awk '{ print $1; }' | xargs -r git branch -d

I've tried different things: enclosing the command with "", adding ! is needed for the pipes... but I cannot make the command work inside the alias. Do you have any idea?

Thanks!

2

There are 2 best solutions below

4
larsks On

Don't bother making it an alias. Just drop it into a file named git-mycommand somewhere in your $PATH (maybe ~/bin); now you can run git mycommand and it will run that script.

Having said that, this might work:

git config --global alias.mycommand '!'"git branch -vv | grep ': gone]'|  grep -v '\*' | awk '{ print $1; }' | xargs -r git branch -d"
1
Enrico Campidoglio On

One alternative is to wrap the commands in a shell function defined inside an alias:

[alias]
name = "!f() { git branch -vv | grep ': gone]'|  grep -v \"\*\" | awk '{ print $1; }' | xargs -r git branch -d; }; f"

An alias that starts with ! will be interpreted as a shell command. In this case, you simply define a shell function named f and then you invoke directly.

Keep in mind that you'll have to escape any " characters inside the function.