Skip to content

Instantly share code, notes, and snippets.

@mbarcia
Last active August 13, 2018 14:50
Show Gist options
  • Save mbarcia/db8735ba8dc526323ca66773ec3c770a to your computer and use it in GitHub Desktop.
Save mbarcia/db8735ba8dc526323ca66773ec3c770a to your computer and use it in GitHub Desktop.
Cleanup of branches already merged
# Git clone the repository
git clone git@github.com:mbarcia/drupsible-project.git
cd drupsible-project/
# Track all remote branches in the local repository
git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs
# Delete those branches already merged into master (in the remote repository)
git branch --merged | grep -v "master" | grep -v "*" | grep -v "\." | xargs -I % git push --delete origin %
# Delete those branches already merged into master (in the local repository)
git branch --merged | grep -v "master" | grep -v "*" | grep -v "\." | xargs -I % git branch -d %
# Prune remote origin
git remote prune origin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment