4

I'm trying to delete all of my merged branches. I've always used

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d but for some reason it won't work anymore, even though I've used this command before. It returns the error "Fatal: branch name required"

Erica Stockwell-Alpert
  • 4,624
  • 10
  • 63
  • 130
  • Try leaving out the `| xargs git branch -d`. If that does not explain it, try leaving out the `egrep` as well, for more illumination. – torek Mar 27 '17 at 20:14

1 Answers1

14

If there is no input provided on stdin -- for example, if the preceding pipeline produces no output -- xargs will run your command with no arguments. That is, if this:

git branch --merged | egrep -v "(^\*|master|dev)"

Produces no output, then xargs will run:

git branch -d

Which, if you were to try that yourself on the command line, produces:

fatal: branch name required

The easiest solution is to add the --no-run-if-empty flag to xargs:

git branch --merged |
 egrep -v "(^\*|master|dev)" |
 xargs --no-run-if-empty git branch -d
larsks
  • 277,717
  • 41
  • 399
  • 399