Skip to content

Instantly share code, notes, and snippets.

@xtqqczze
Last active October 1, 2025 21:33
Show Gist options
  • Select an option

  • Save xtqqczze/047dd27ddc5bc5802e12d9a84a8f52e4 to your computer and use it in GitHub Desktop.

Select an option

Save xtqqczze/047dd27ddc5bc5802e12d9a84a8f52e4 to your computer and use it in GitHub Desktop.
Delete all remote Git branches
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment