Today I Learned

Delete All Git Branches Which Have Been Merged

To delete all branches that are already merged into the currently checked out branch:

git branch --merged | grep -v "\*" | grep -v master | grep -v dev | xargs -n 1 git branch -d

You can see that master and dev are excluded in case they are an ancestor.

You can delete a merged local branch with:

git branch -d branchname

If it’s not merged, use:

git branch -D branchname

To delete it from the remote in old versions of Git use:

git push origin :branchname

In more recent versions of Git use:

git push --delete origin branchname

Once you delete the branch from the remote, you can prune to get rid of remote tracking branches with:

git remote prune origin

or prune individual remote tracking branches, as the other answer suggests, with:

git branch -dr branchname

To delete all branches on remote that are already merged:

git branch -r --merged | grep -v master | sed 's/origin\///' | xargs -n 1 git push --delete origin