Git remove local branches that don’t exist remote

Use the following to have the branches displayed before you’re asked to delete them.

branches=$(git branch -r | awk '{print $1}' \
  | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) \
  | awk '{print $1}' | xargs); \
printf '\n\nBranches to be removed:\n---\n'; \
echo ${branches} | xargs -n1; \
printf '---\n\nRemove the branches above? [Ny] ' \
    &&  read shouldDelete \
    && [[ "${shouldDelete}" =~ [yY] ]] \
      && echo $branches | xargs git branch -d \
      || echo 'aborted' 

Click Here to Leave a Comment Below

Leave a Reply: