Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Delete all remote branches, except master
# Replace REMOTE_NAME with your remote name (e.g. origin)
git branch -r | grep REMOTE_NAME/ | grep -v 'master$' | grep -v HEAD| cut -d/ -f2 | while read line; do git push REMOTE_NAME :$line; done;
@drexler

This comment has been minimized.

Copy link

@drexler drexler commented Feb 8, 2018

This is a life saver! Sweet!!

@GongT

This comment has been minimized.

Copy link

@GongT GongT commented May 7, 2018

may replace cut -d/ -f2 with sed 's#^\s*REMOTE_NAME/##g', there can be a slash in branch name

@breeswish

This comment has been minimized.

Copy link

@breeswish breeswish commented May 30, 2018

in MacOS, use sed -E "s/^[[:space:]]*REMOTE_NAME\///g", also git push command could be git push REMOTE_NAME :heads/$line in case of tags with same name.

For copy-pasters:

REMOTE_NAME=xxxxx
git branch -r | grep "${REMOTE_NAME}/" | grep -v 'master$' | grep -v HEAD | sed -E "s/^[[:space:]]*${REMOTE_NAME}\///g" | while read line; do git push $REMOTE_NAME :heads/$line; done;
@TimoRoth

This comment has been minimized.

Copy link

@TimoRoth TimoRoth commented Jul 22, 2018

Just use -f2- instead of just -f2 to fix the branches with / in them.

@mbdevpl

This comment has been minimized.

Copy link

@mbdevpl mbdevpl commented Aug 21, 2018

Hmm... but git push accepts many branches at the same time (e.g. git push origin :devel :experimental :blah), so you can simply do:

REMOTE="origin"
git branch -r |  grep "^  ${REMOTE}/" | sed "s|^  ${REMOTE}/|:|" | grep -v "^:HEAD" | grep -v "^:master$" | xargs git push ${REMOTE}
@Moriarty16

This comment has been minimized.

Copy link

@Moriarty16 Moriarty16 commented Sep 7, 2018

Perfect idea. Save a lot of work.
And to be cautious, perhaps do git branch -r | grep REMOTE_NAME/ | grep -v 'master$' | grep -v HEAD| cut -d/ -f2 | while read line; do echo $line; done; before delete.

@apoorv-mishra

This comment has been minimized.

Copy link

@apoorv-mishra apoorv-mishra commented Oct 8, 2018

Might want to try git branch -r | grep origin/ | grep -v 'master$' | grep -v HEAD | cut -d/ -f2 | parallel git push origin --delete, which is faster. Refer https://www.gnu.org/software/parallel/.

@kirtangajjar

This comment has been minimized.

Copy link

@kirtangajjar kirtangajjar commented Nov 23, 2018

@leoapost This command does not work for branches with / in its name. To fix this, you need to replace cut -d/ -f2 with cut -d/ -f2-

git branch -r | grep REMOTE_NAME/ | grep -v 'master$' | grep -v HEAD| cut -d/ -f2- | while read line; do git push REMOTE_NAME :$line; done;
@xhliu

This comment has been minimized.

Copy link

@xhliu xhliu commented Jan 7, 2019

Works gr8, thanks 4 sharing.

@vinayf

This comment has been minimized.

Copy link

@vinayf vinayf commented Feb 21, 2019

Sweeeeet, thanks for sharing!

@VishwasShashidhar

This comment has been minimized.

Copy link

@VishwasShashidhar VishwasShashidhar commented Jun 4, 2019

This is great! Thank you for sharing!

@mohshannan

This comment has been minimized.

Copy link

@mohshannan mohshannan commented Jul 15, 2019

Thanks for sharing

@marnee01

This comment has been minimized.

Copy link

@marnee01 marnee01 commented Jan 29, 2020

Thanks to the OP and others who added additional info. Very helpful!

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