Skip to content

Instantly share code, notes, and snippets.

@tsnow
Created October 6, 2013 19:34
Show Gist options
  • Select an option

  • Save tsnow/6858168 to your computer and use it in GitHub Desktop.

Select an option

Save tsnow/6858168 to your computer and use it in GitHub Desktop.
working with many many remotes, e.g. emacs starter kit, dotfiles, gitx forks, etc
# delete (locally) remote branches older than 2013 ("not" 2013)
git branch -r | while read i; do git log -n 1 $i --pretty=format:"%ci" | grep -e '^2013' && echo $i || git branch -r -d $i; done
# delete (locally) remote branches which are fully merged into another remote branch
git branch -r | grep -v origin/HEAD | while read branch; do git branch -r --merged $branch | grep -v $branch; done | sort | uniq | while read rm_branch; do git branch -r -d $rm_branch; done
# Remove (locally) all branches on all remotes which are merged into their own $origin/master. Set that remote to only sync those branches.
git remote | while read i; do git ls-remote -h $i | sed "s,.*refs/heads/,$i/,"; done > all_heads;
git remote | while read i; do git branch -r --merged $i/master | grep $i | grep -v master; done > up_to_date_with_master;
git remote | while read i; do grep $i all_heads > branch_heads; grep $i up_to_date_with_master >> branch_heads; sort branch_heads | uniq -u | sed "s,$i/,," > branch_heads_keep; uniq -d branch_heads_keep | while read j; do git branch -r -d $i/$j; done; cat branch_heads_keep | xargs git remote set-branches $i; git remote prune $i; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment