Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
delete all remote branches that have already been merged into master
$ git branch -r --merged |
grep origin |
grep -v '>' |
grep -v master |
xargs -L1 |
awk '{split($0,a,"/"); print a[2]}' |
xargs git push origin --delete

catsby commented Apr 26, 2011

How about awk '{sub(/origin\//,"");print}' instead for line 6. This will give you the full branch name omitting origin. Works in situation where someone is using git-flow and has feature branches named feature/a-cool-feature

jbbarth commented Apr 26, 2011

awk '{split($0,a,"/"); print a[2]}' might be replaced with cut -d"/" -f 2 for readibility (I presume cut is available on MacOSX as well as any Linux distribution)

https://gist.github.com/942981 handles the embedded slash. Anyone know the right conditions to get --merged showing the > line?

Now 400% more awky, and down to 3 lines https://gist.github.com/943565

catsby commented Apr 27, 2011

@jbbarth @malclocke those solutions do not work for a project using git-flow, who's branch names are like @

mpictor commented Jul 14, 2012

If you run this while on a branch that tracks a remote branch and is not master, this script will delete the remote branch!

Change git branch -r --merged to git branch -r --merged origin/master to eliminate this.

I regularly create branches named mp/some-branch or review/some-branch. As a result, I vote for the change @ctshryock suggests. :D

One last change - I added a line at the top of the file, git remote prune origin, which cleans up local records of any remote branches that no longer exist.

git branch -r --merged | 
grep origin | 
grep -v '>' | 
grep -v master | 
xargs -L1 | 
cut -d"/" -f2- | 
xargs git push origin --delete

young40 commented Oct 15, 2014

greate

I am using git-fiow and I dont wanna delete the origin/develop?

rtpg commented Sep 29, 2015

@guneysus : replace grep -v master with grep -v develop

If you get a error: unable to delete 'origin/myBranch-1234': remote ref does not exist, run git fetch -p origin before.

I actually added that command to my version of the script (note that it's for branches from develop):

git fetch -p origin && git branch -r --merged | grep origin |grep -v '>' | grep -v develop | xargs -L1 | cut -d"/" -f2- | xargs git push origin --delete

Emuentes commented May 16, 2016 edited

I tried this,
https://gist.github.com/Emuentes/80c96c3927911dae6e19

Works for me, I also use git-flow @guneysus

zsoobhan commented Oct 11, 2016 edited

@arielelkin I had the same issue. My solution was to run it in a for loop.

for branch in $(git branch -r --merged master | grep origin | grep -v develop | grep -v master | sed -E "s|^ *origin/||g")
do
    git push origin $branch --delete
done

https://gist.github.com/zsoobhan/53b598da50a5496f655a07bb9fb39151

For clean projects using /feature or /fix.

$ git fetch --prune;
$ git branch --remote --merged |
    grep origin |
    grep -v '>' |
    grep -v master |
    grep -v develop |
    xargs -L1 |
    cut -d"/" -f2- |
    xargs git push origin --delete;

Dorian commented Dec 7, 2016

Deletes merged / deleted branches, works with branch with / in their names etc.

git fetch --prune; git branch -r --merged | grep origin | grep -v '>' | grep -v master | xargs -L1 | awk '{sub(/origin\//,"");print}' | xargs git push origin --delete

mcplectrum commented Apr 12, 2017 edited

git fetch --prune; git branch -r --merged | grep origin | grep -v '>' | grep -v master | xargs -L1 | awk '{sub(/origin//,"");print}' | xargs git push origin --delete

In my case this would also delete the branches {development, release}, which is probably not what you want.

starkcoffee commented May 10, 2017 edited

awk is cool but a bit overkill. sed is cooler.

git branch -r --merged | grep origin | grep -v '>' | grep -v master | sed 's/origin\///' | xargs git push origin --delete

kalkin commented Aug 31, 2017

sed is fine, but cut is more appropriate here. Also when using egrep it get shorter:

git branch -r --merged | grep origin | egrep -v '>|master' | cut -d/ -f2- | xargs git push origin --delete

I use sed so I am not limited to origin.
git fetch --all --prune && git branch --remote --merged | grep -v -P 'master|develop$' | sed -e 's/\// /g' | xargs -L1 -r git push -d; echo Done cleaning remote branches.

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