mfclean 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/env bash
  2. #
  3. # mfclean
  4. #
  5. # Prune all your merged branches and any branches whose remotes are gone
  6. # Great way to clean up your branches after messing around a lot
  7. #
  8. AWK=$(which gawk || which awk)
  9. KEEP="RC|RCBugFix|dev|master|bugfix-1|bugfix-2"
  10. echo "Fetching latest upstream and origin..."
  11. git fetch upstream
  12. git fetch origin
  13. echo
  14. echo "Pruning Merged Branches..."
  15. git branch --merged | egrep -v "^\*|$KEEP" | xargs -n 1 git branch -d
  16. echo
  17. echo "Pruning Remotely-deleted Branches..."
  18. git branch -vv | egrep -v "^\*|$KEEP" | grep ': gone]' | "$AWK" '{print $1}' | xargs -n 1 git branch -D
  19. echo
  20. # List fork branches that don't match local branches
  21. echo "You may want to remove (or checkout) these refs..."
  22. comm -23 \
  23. <(git branch --all | sed 's/^[\* ] //' | grep origin/ | grep -v "\->" | awk '{ print $1; }' | sed 's/remotes\/origin\///') \
  24. <(git branch --all | sed 's/^[\* ] //' | grep -v remotes/ | awk '{ print $1; }') \
  25. | awk '{ print "git branch -d -r origin/" $1; print "git checkout origin/" $1 " -b " $1; print ""; }'
  26. echo