Skip to content

Instantly share code, notes, and snippets.

@PartyLich
Forked from ericelliott/gitclean.sh
Last active December 3, 2019 18:19
Show Gist options
  • Save PartyLich/9be71746872bb2ec6a661a7378ebdfba to your computer and use it in GitHub Desktop.
Save PartyLich/9be71746872bb2ec6a661a7378ebdfba to your computer and use it in GitHub Desktop.
gitclean.sh - cleans merged/stale branches from origin
#!/bin/bash
# targetBranch=${2:-$(git rev-parse --abbrev-ref HEAD)}
# echo "target branch is '$targetBranch'"
remote=${1:-origin}
git remote prune $remote
# get remote merged branches
git branch -r --merged master | \
# remove master or develop case-insensitive
egrep -iv '(master|develop|dev)' | \
# replace remote/ with '' (nothing)
sed "s/$remote\///g" | \
# use n=1 args with remote branch delete command
xargs -n 1 git push --delete $remote
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment