mfclean 1017 B

123456789101112131415161718192021222324252627282930
  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. KEEP="RC|RCBugFix|dev|master|bugfix-1|bugfix-2"
  9. echo "Fetching latest upstream and origin..."
  10. git fetch upstream
  11. git fetch origin
  12. echo
  13. echo "Pruning Merged Branches..."
  14. git branch --merged | egrep -v "^\*|$KEEP" | xargs -n 1 git branch -d
  15. echo
  16. echo "Pruning Remotely-deleted Branches..."
  17. git branch -vv | egrep -v "^\*|$KEEP" | grep ': gone]' | gawk '{print $1}' | xargs -n 1 git branch -D
  18. echo
  19. # List fork branches that don't match local branches
  20. echo "You may want to remove (or checkout) these refs..."
  21. comm -23 \
  22. <(git branch --all | sed 's/^[\* ] //' | grep origin/ | grep -v "\->" | awk '{ print $1; }' | sed 's/remotes\/origin\///') \
  23. <(git branch --all | sed 's/^[\* ] //' | grep -v remotes/ | awk '{ print $1; }') \
  24. | awk '{ print "git branch -d -r origin/" $1; print "git checkout origin/" $1 " -b " $1; print ""; }'
  25. echo