Skip to content

Instantly share code, notes, and snippets.

@gregoryjscott
Created November 12, 2012 23:11
Show Gist options
  • Save gregoryjscott/4062692 to your computer and use it in GitHub Desktop.
Save gregoryjscott/4062692 to your computer and use it in GitHub Desktop.
delete merged branches
# define the branches to ignore
ignoredBranches="master\|staging\|production\|HEAD"
# run from master
git checkout master
# update list of remotes
git fetch
git remote prune origin
# remove local fully merged branches
git branch --merged master |
grep -v ${ignoredBranches} |
xargs git branch -d
# show remote fully merged branches
echo "The following remote branches are fully merged and will be removed:"
git branch -r --merged master |
sed 's/ *origin\///' |
grep -v ${ignoredBranches}
read -p "Continue (y/n)? "
if [ "$REPLY" == "y" ]
then
# remove remote fully merged branches
git branch -r --merged master |
sed 's/ *origin\///' |
grep -v ${ignoredBranches} |
xargs -i% git push origin :heads/%
echo "Done!"
fi
@gregoryjscott
Copy link
Author

The -i option passed to xargs (line 29) needs to be lowercase in Git Bash on Windows (uppercase I throws error), but needs to be uppercase in Bash on OS X (lowercase i throws error).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment