Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@malclocke
Forked from schacon/gist:942899
Created April 27, 2011 01:31
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save malclocke/943565 to your computer and use it in GitHub Desktop.
Save malclocke/943565 to your computer and use it in GitHub Desktop.
delete all remote branches that have already been merged into master
$ git branch -r --merged |
awk -F'/' '/^ *origin/{if(!match($0, /(>|master)/)){print $2}}' |
xargs git push origin --delete
@talsafran
Copy link

Thanks for sharing this! I'm getting the following error though:

xargs: illegal option -- r
usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]
             [-L number] [-n number [-x]] [-P maxprocs] [-s size]
             [utility [argument ...]]

Anyone else getting this? Running on MacOS Sierra.

@TrevorSayre
Copy link

TrevorSayre commented Feb 17, 2017

@talsafran Yep, -r is not an option on MacOS xargs (see: man xargs)
-r is a GNU extension

You can install GNU xargs through Homebrew and the GNU findutils package.
brew install findutils

This will give you GNU xargs as gxargs, and you can use the -r option

The same goes for other basic commands found in the findutils package such as gfind or glocate or gupdatedb, which have different BSD counterparts on OS X.

After installing findutils, the command on MacOS will be:

git branch -r --merged origin/master \
    | awk -F/ '/^\s*origin/ {if (!match($0, /origin\/master/)) {sub("^\\s*origin/", ""); print}}' \
    | gxargs -rpn1 git push origin --delete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment