Source: https://stackoverflow.com/a/6127884/3523645
git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d
Problem: If pull-request are merged into the main branch using a "squash" strategy and then the remote branches are removed,
local branches will not show up as "merged" (git branch --merged
will not show them).
These branches can still be sorted out (https://stackoverflow.com/a/47939403/3523645)
First, we need to delete remote refs that are no longer in use on the remote repository:
git fetch --prune
Now, git will know which local branches have their remotes deleted. So if we do this:
git branch --verbose
We will see those branches marked with [gone]
.
To remove them, we can run something like this:
git branch --verbose | grep "\[gone\]" | cut -d " " -f3 | xargs git branch -D