Skip to content

Instantly share code, notes, and snippets.

@leoapost
Created December 17, 2012 13:55
Show Gist options
  • Save leoapost/4318441 to your computer and use it in GitHub Desktop.
Save leoapost/4318441 to your computer and use it in GitHub Desktop.
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
Copy link

drexler commented Feb 8, 2018

This is a life saver! Sweet!!

@GongT
Copy link

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

@breezewish
Copy link

breezewish 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
Copy link

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

@mbdevpl
Copy link

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
Copy link

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
Copy link

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
Copy link

@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
Copy link

xhliu commented Jan 7, 2019

Works gr8, thanks 4 sharing.

@vinayf
Copy link

vinayf commented Feb 21, 2019

Sweeeeet, thanks for sharing!

@VishwasShashidhar
Copy link

This is great! Thank you for sharing!

@mohshannan
Copy link

Thanks for sharing

@marnee01
Copy link

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

@jmsalcido
Copy link

thanks

@xtqqczze
Copy link

xtqqczze commented Sep 30, 2025

#!/bin/sh
# git-purge-remote.sh
# Deletes all remote branches for a given remote, except HEAD and main.

# Name of the remote to purge. Change if needed.
REMOTE_NAME='origin'

# List all remote branches for the given remote, excluding HEAD and main.
# Pipe the branch names to git push to delete them
#   xargs -r prevents git push from running if there are no branches
git for-each-ref \
    --format='%(refname:strip=3)' \
    --exclude="refs/remotes/$REMOTE_NAME/HEAD" \
    --exclude="refs/remotes/$REMOTE_NAME/main" \
    "refs/remotes/$REMOTE_NAME" \
| xargs -r git push "$REMOTE_NAME" --delete

https://gist.github.com/xtqqczze/047dd27ddc5bc5802e12d9a84a8f52e4

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