Delete all local branches which are merged to master (or whatever branch you choose to checkout)
git checkout master
git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d
Check which local branches are left and delete some of them manually:
git branch
git branch -D <names of the branches you want to delete>
# Example:
# git branch -D my-old-unmerged-branch-1 some-old-unmerged-branch-2 another-old-unmerged-branch-3
By default, Git keeps hold of tracking branches, even if they no longer exist in their corresponding remotes.
Let's tell Git to clear them out if they don't exist in the remote using the -p
flag:
git fetch -p origin
If you have any other remotes besides origin
, make sure to run this command for those too!
Before doing any changes, let's have a look at what merged branches we can prune from origin/master
.
This won't delete anything, it just prints out which branches the command would delete:
git branch -r --merged | grep -v master | grep origin | sed 's/origin\//:/' | xargs -n 1 echo
If you're happy with those changes, let's delete all those branches from remote origin:
Note: if you have multiple remotes, you might need to add | grep origin
after grep -v master
to prevent it from pushing branches of other remotes to origin
git branch -r --merged | grep -v master | sed 's/origin\///' | xargs -n 1 git push --delete origin
The command above goes through each remote branch and deletes them sequentially, so it might take a while if you have hundreds of branches π€·ββοΈ
Source: Stackoverflow - using answers from Adam Dymitruk
, kuboon
and L0LN1NJ4
and Alex Arriaga