When you are working in a development team for a project, there are heaps of branches on the remote git repository for PR or whatever reasons. Usually the branch would be automatically delete after that PR got merged into master on the repository hosting platform. And then you might notice your local has heaps of branches that were "abandoned".

This bash script can check what branches had been merged and try to delete them. Script gist: https://gist.github.com/pingcheng/136f404e86761491f7b813a5632b4720

#!/bin/sh

# get all merged branch
branches=$(git branch --merged master | grep -v '^[ *]*master$')

# define colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
LIGHT_GRAY='\e[90m'
NC='\033[0m'

# if no branches are need to clear, just exit
if [ -z "$branches" ]
then
        echo "${GREEN}No merged branches to remove${NC}"
        exit 0
fi

# delete each merged branch
for branch in $branches
do
        echo "${LIGHT_GRAY}Deleting merged branch - ${YELLOW}${branch}${NC}"
        git branch -d $branch
done

echo "${GREEN}Done!"