-
-
Save leoapost/4318441 to your computer and use it in GitHub Desktop.
| # 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; |
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.
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/.
@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;
Works gr8, thanks 4 sharing.
Sweeeeet, thanks for sharing!
This is great! Thank you for sharing!
Thanks for sharing
Thanks to the OP and others who added additional info. Very helpful!
thanks
#!/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" --deletehttps://gist.github.com/xtqqczze/047dd27ddc5bc5802e12d9a84a8f52e4
Hmm... but git push accepts many branches at the same time (e.g.
git push origin :devel :experimental :blah), so you can simply do: