Pruning Git Branches

- - posted in Uncategorized

Git branches are cheap and fun… until you have so many you can’t remember which branches have been merged into develop.

Here’s a little script that will find and delete local & remote branches that have been merged into develop or master:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
\# This has to be run from master  
git checkout develop

\# Update our list of remotes  
git fetch  
git remote prune origin

\# Remove local fully merged branches  
git branch –merged develop | grep -v ‘develop$’ | grep -v ‘master$’ | 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 develop | sed ‘s/ *origin\///’ | grep -v ‘develop$’ | grep -v ‘master$’

read -p “Continue (y/n)? ”  
if [ "$REPLY" == "y" ]  
then  
\# Remove remote fully merged branches  
git branch -r –merged develop | sed ‘s/ *origin\///’ \  
| grep -v ‘develop$’ | grep -v ‘master$’ | xargs -I% git push origin :%  
echo “Done!”