1

I'm fairly new to git and I wanted to have an alias for this command but I'm having some trouble.

git fetch -p &&git branch -vv|grep ': gone]'|awk '{print $1}'|xargs git branch -D

This is a function from the question here. Remove local branches no longer on remote

The function works perfectly for me, but when I try to put it in an alias, it runs but doesn't work. Any suggestions would be greatly appreciated. This is what it looks like in my config.

[alias]
deletedone = "!f() { git fetch -p &&git branch -vv|grep ': gone]'|awk '{print $1}'|xargs git branch -D;}"
Rumid
  • 1,627
  • 2
  • 21
  • 39

2 Answers2

1

You just need to add "; f" in the definition of the alias.

That is to say:

[alias] deletedone = "!f() { git fetch -p && git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D; }; f"

or what amounts to the same, running the command:

git config alias.deletedone '!f() { git fetch -p && git branch -vv | grep '\'': gone]'\'' | awk '\''{print $1}'\'' | xargs git branch -D; }; f'

or to make the alias global (independently of any repo):

git config --global alias.deletedone '!f() { git fetch -p && git branch -vv | grep ": gone]" | awk '\''{print $1}'\'' | xargs git branch -D; }; f'

Edit: As the original version of the alias was not robust enough, as pointed out in this comment, I made a few tweaks to finally obtain the following version:

git config --global alias.deletedone '!f() { git fetch -p && git branch -vv | \
  perl -wne '\''print "$1\n" if m/^\s*(\S+)\s+[0-9a-f]+\s+\[\S+: gone\]/;'\'' | \
  xargs git branch -d; }; f'
ErikMD
  • 13,377
  • 3
  • 35
  • 71
1

You're defining a function but never running it. There isn't really a need to define one at all as far as I can see.

git config --global alias.deletedone '!bash -c "git fetch -p && git branch -vv | grep \': gone]\' | awk \'{print $1}\' | xargs git branch -D" -'

Not at a computer where I can test this, but it should do what you want to do (possible there is a syntax error in there somewhere).

Calling out to a fresh bash instance will put it in its own scope when calling outside of git. I've had weird scope issues when I don't use this syntax.

LightBender
  • 4,046
  • 1
  • 15
  • 31