Skip to content

Instantly share code, notes, and snippets.

@BenGitsCode
Forked from ericelliott/gitclean.sh
Created June 14, 2017 23:54
Show Gist options
  • Save BenGitsCode/7db521aac61f45517b2b2e23c62f6cdb to your computer and use it in GitHub Desktop.
Save BenGitsCode/7db521aac61f45517b2b2e23c62f6cdb 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
@BenGitsCode
Copy link
Author

BenGitsCode commented Jun 14, 2017

  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

more generic version credit due to @BerkeleyTrue https://github.com/BerkeleyTrue/dotfiles/blob/master/dotfiles/profile#L236

original source: https://gist.github.com/ericelliott/2271bd12736585175c85

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