Home > Software engineering >  How to git alias this git complex git command with single and double quotes inside
How to git alias this git complex git command with single and double quotes inside

Time:02-26

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!

CodePudding user response:

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"

CodePudding user response:

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.

  • Related