0

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!

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Sergiodela
  • 43
  • 6

2 Answers2

4

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"
larsks
  • 277,717
  • 41
  • 399
  • 399
  • That command does not work as this is what writes in the .gitconfig file and the behaviour is slightly different `[alias] mycommand = "!git branch -vv | grep ': gone]'| grep -v '\\*' | awk '{ print ; }' | xargs -r git branch -d"'` – Sergiodela Feb 25 '22 at 16:30
  • it behaves differently because of this filtering whis is different from the original command - grep -v '\\*' – Sergiodela Feb 25 '22 at 16:40
  • 1
    Well, you can fix that :). But I think the non-alias solution is going to be much easier. – larsks Feb 25 '22 at 17:15
  • There's a rule, I think attributed to Kernighan, that says that when you're debugging, you only get to use about half your intelligence on the *bug* while using half on the overall problem. So never make your code too clever as you will find it impossible to debug later. :-) In other words, aim for clarity (a script) over cleverness / efficiency (an alias)... – torek Feb 25 '22 at 23:14
0

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.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • Hi, copy pasting that into the .gitconfig gives bad config error when trying to use git, I think that is not correct. did you try it? Thanks! – Sergiodela Feb 25 '22 at 16:33