Skip to content

Instantly share code, notes, and snippets.

@dresselm
Last active August 29, 2015 13:56
Show Gist options
  • Save dresselm/9174695 to your computer and use it in GitHub Desktop.
Save dresselm/9174695 to your computer and use it in GitHub Desktop.
#!/bin/sh
function print_header() {
echo "\n------------------------------------------------------------------------"
echo $1
echo "------------------------------------------------------------------------\n"
}
# Checkout master to ensure that we are dealing with topic branches in our cleanup
git co master
# Get the latest remote changes
git fetch
print_header "Cleanup local references to deleted remote branches"
git remote prune origin --dry-run
print_header "Local branches already merged into master"
git branch --merged master | grep -v "\*"
# To delete these automatically
# git branch --merged master | grep -v "\*" | xargs -n 1 git branch -d
print_header "Remote branches already merged into origin/master"
git branch -r --merged origin/master | grep -v "origin\/HEAD"
# git branch --merged master | grep -v "origin\/HEAD" | xargs -n 1 git branch -d
# Once the above are cleaned up, we can see if there are any orphaned branches: not merged and stale
print_header "Local branches sorted by last commit date"
# Sort all local branches by latest commit date
git for-each-ref --sort=-committerdate --format='%(committerdate:short) | %(refname) | %(objectname:short) | %(authorname)' refs/heads/ | sed 's/refs\/.*\///g'
print_header "Remote branches sorted by last commit date"
# Sort all remote branches by latest commit date
git for-each-ref --sort=-committerdate --format='%(committerdate:short) | %(refname) | %(objectname:short) | %(authorname)' refs/remotes/ | sed 's/refs\/.*\///g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment