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

This comment has been minimized.

Show comment Hide comment
@catsby

catsby 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

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

This comment has been minimized.

Show comment Hide comment
@jbbarth

jbbarth 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)

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)

@matschaffer

This comment has been minimized.

Show comment Hide comment
@matschaffer

matschaffer Apr 27, 2011

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

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

@malclocke

This comment has been minimized.

Show comment Hide comment
@malclocke

malclocke Apr 27, 2011

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

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

@catsby

This comment has been minimized.

Show comment Hide comment
@catsby

catsby Apr 27, 2011

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

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

This comment has been minimized.

Show comment Hide comment
@mpictor

mpictor 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.

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.

@stevenkampen

This comment has been minimized.

Show comment Hide comment
@stevenkampen

stevenkampen Aug 12, 2014

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

This comment has been minimized.

Show comment Hide comment
@young40

young40 Oct 15, 2014

greate

young40 commented Oct 15, 2014

greate

@guneysus

This comment has been minimized.

Show comment Hide comment
@guneysus

guneysus Sep 16, 2015

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

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

@rtpg

This comment has been minimized.

Show comment Hide comment
@rtpg

rtpg Sep 29, 2015

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

rtpg commented Sep 29, 2015

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

@arielelkin

This comment has been minimized.

Show comment Hide comment
@arielelkin

arielelkin Feb 4, 2016

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

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

This comment has been minimized.

Show comment Hide comment
@Emuentes

Emuentes May 16, 2016

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

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

Emuentes commented May 16, 2016

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

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

@zsoobhan

This comment has been minimized.

Show comment Hide comment
@zsoobhan

zsoobhan Oct 11, 2016

@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

zsoobhan commented Oct 11, 2016

@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

@Webysther

This comment has been minimized.

Show comment Hide comment
@Webysther

Webysther Oct 14, 2016

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;

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

This comment has been minimized.

Show comment Hide comment
@Dorian

Dorian 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

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

This comment has been minimized.

Show comment Hide comment
@mcplectrum

mcplectrum Apr 12, 2017

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.

mcplectrum commented Apr 12, 2017

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

This comment has been minimized.

Show comment Hide comment
@starkcoffee

starkcoffee May 10, 2017

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

starkcoffee commented May 10, 2017

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

This comment has been minimized.

Show comment Hide comment
@kalkin

kalkin 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

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

@spezifanta

This comment has been minimized.

Show comment Hide comment
@spezifanta

spezifanta Oct 16, 2017

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.

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.

@HerveNivon

This comment has been minimized.

Show comment Hide comment
@HerveNivon

HerveNivon Dec 21, 2017

For the speedy users, do not forget the common develop branch

For the speedy users, do not forget the common develop branch

@voiski

This comment has been minimized.

Show comment Hide comment
@voiski

voiski Jan 31, 2018

Hi, thanks all for the tip to drop a huge list of merged branches but I believe that we can save time pushing all branches together to have one unique transaction:

git push origin --delete $(git branch -r --merged origin/master |  grep origin | egrep -v '>|master|develop' | cut -d/ -f2-)

voiski commented Jan 31, 2018

Hi, thanks all for the tip to drop a huge list of merged branches but I believe that we can save time pushing all branches together to have one unique transaction:

git push origin --delete $(git branch -r --merged origin/master |  grep origin | egrep -v '>|master|develop' | cut -d/ -f2-)
@dimified

This comment has been minimized.

Show comment Hide comment
@dimified

dimified Feb 9, 2018

I like your solution @voiski. Can this somehow also be improved to avoid this message when no refs exist:

fatal: --delete doesn't make sense without any refs

dimified commented Feb 9, 2018

I like your solution @voiski. Can this somehow also be improved to avoid this message when no refs exist:

fatal: --delete doesn't make sense without any refs

@gustavosotnas

This comment has been minimized.

Show comment Hide comment
@gustavosotnas

gustavosotnas May 8, 2018

Best answer in my opinion @voiski 👏 👏 👏

Best answer in my opinion @voiski 👏 👏 👏

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