Removing Merged git Branches

If you're like me, your git branches can get out of control over time. Here's some bash sorcery which will give a list of all branches that have been merged to origin/master.


by flipperpa on Jan. 17, 2020, 4:30 p.m.

Python How-To Linux

This is my attempt to put together a bash routine to clean up my remote and local git branches.

It should:

For anyone feeling confident that this script works for them, I've also included commands to delete the branches locally and on the remote repository commented out.

Your mileage may vary... use with extreme care!

for k in $(
    git branch --remote --merged origin/master | fgrep origin | grep -v -E -- 'develop|stage|master' | sed /\*/d
); do 
  if (($(git log -1 --since='4 weeks ago' -s $k|wc -l)==0)); then
    # Use bash to replace 'origin/' with '' and assign to $branch
    branch=${k/origin\//}
    echo $branch
    # git push origin --delete $branch
    # git branch -d $branch
  fi
done