Skip to content

Instantly share code, notes, and snippets.

@ericelliott
Created January 31, 2016 04:57
Show Gist options
  • Save ericelliott/2271bd12736585175c85 to your computer and use it in GitHub Desktop.
Save ericelliott/2271bd12736585175c85 to your computer and use it in GitHub Desktop.
gitclean.sh - cleans merged/stale branches from origin
git remote prune origin
git branch -r --merged master | egrep -iv '(master|develop)' | sed 's/origin\///g' | xargs -n 1 git push --delete origin
@BerkeleyTrue
Copy link

Noice! Had to make it a little more generic for my uses:

  currentbranch=$(git rev-parse --abbrev-ref HEAD)
  remote=${1:-origin}
  echo "current branch is '$currentbranch'"
  echo "pruning remote '$remote' refs that no longer"
  git remote prune $remote
  # List out the merged branches
  echo "deleting remote branches merged into '$currentbranch'"
  git branch -r --merged $currentbranch |\
    # filter out everything that has the same name as the current branch
    egrep -iv "$currentbranch" |\
    # grab only those of the current remote
    egrep -i "$remote/" |\
    # filter out remote name from the string
    sed "s/$remote\///g" |\
    # push and delete the branch
    xargs -n 1 git push --delete $remote

https://github.com/BerkeleyTrue/dotfiles/blob/master/dotfiles/profile#L236

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