Git remove local branches that don’t exist remote

The quick way:

git branch --merged master | grep -v '^[ *]*master$' | xargs git branch -d
git remote prune origin

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

branches=$(git branch --merged master | grep -v '^[ *]*master$'); \
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' 

source: https://stackoverflow.com/a/16906759

Click Here to Leave a Comment Below

Leave a Reply: