Skip to content

Instantly share code, notes, and snippets.

@trobrock
Created July 31, 2012 18:40
Show Gist options
  • Save trobrock/3219365 to your computer and use it in GitHub Desktop.
Save trobrock/3219365 to your computer and use it in GitHub Desktop.
Clean unneeded branches from a git remote
#!/usr/bin/env bash
git_clean_remote()
{
remote=$1
[[ -z "$remote" ]] && echo "Must provide a remote" && return 1
for branch in $(git branch -r | grep $remote | grep -v master)
do
diff=$(git cherry -v master $branch | wc -l)
short_branch=$(echo $branch | cut -d'/' -f2)
if [ "$diff" -eq 0 ]; then
git push $remote :$short_branch > /dev/null 2>&1
git branch -d $short_branch > /dev/null 2>&1
echo "Deleted branch $branch"
else
echo "Can't delete $branch"
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment