I always look up on stackOverflow for this one. Most of the company I've worked in use github or some similar software where the remote feature branch is automatically deleted when you merge your PR.

This is great for housekeeping and keeping your repo clean, but it doesn't happen magically on your computer. There you have 2 choices :

  • you delete your branch manually whenever a PR is merged. It's the cleanest way, but you need some discipline !
  • you have a script which goes through your current branch and delete them base on criteria (which can be precarious sometimes, relying on a commit msg for example)

I prefer the second option as I can automate the process through this script:

git fetch -p && for branch in $(git branch -vv | grep ': gone]' | awk '{print $1}'); do git branch -D $branch; done

several steps here :

git fetch -p

remove all the remote reference that no longer exists (making sure your local branch knows that their remote counterpart and reference are gone)

&& for branch in $(git branch -vv | grep ': gone]' | awk '{print $1}');

The -vv allow to list the branch with extra information (stand for verbose) - displaying if the branch is gone. We then rely on grep to get to match to select the branch on : gone] and using awk to pass it to the next part of the script

Side Note : this can be fragile, as if the last commit of the branch includes : gone] it will delete it.

do git branch -D $branch; done

delete the corresponding branch

Credit to stackoverFlow thread

;