Day 5 - Trim your branches

I have some bash scripts that I’ve been using for a while for this

#!/bin/bash

branches=$(git branch --merged | grep -v '*' | grep -v master)
length=${#branches[@]}

if [[ $branches != '' && $length > 0 ]]; then
  echo $branches | xargs git branch -d
else
  echo Merged branches not found
fi

branches=$(git fetch -p && git branch -vv | grep -v '^*' | awk '/: gone]/{print $1}')
length=${#branches[@]}

if [[ $branches != '' && $length > 0 ]]; then
  echo $branches | xargs git branch -D
else
  echo No branches with missing remotes
fi

which would automatically remove merged branches and branches with missing remotes (usually merged but with rebased commit)… saved as git-localprune so that I can run it as git localprune

and then another bash script to also take care of pruning remote which looks like

#!/bin/bash

git pull
git remote prune origin
git localprune

saved as git-prunsuka… so everytime I need to pull master, I do git prunsuka instead of git pull:smiley:

NB: prunsuka is a combination of prune and punsuka (プンスカ)which is japanese onomatopoeia for / (╯°□°)╯︵ ┻━┻

7 Likes