- First - fetch and prune
- Second - delete merged local branches
When actively developing feature branches that are later merged into develop branch you might end-up with bunch of local branches that are not relevant anymore, does not have their remote counterparts and it would be good to remove them locally.
First - fetch and prune
$ git fetch -p
git fetch --prune will connect to the remote and fetch
the latest remote state before pruning. It is essentially a
combination of commands:
git fetch --all && git remote prune
NOTE: The generic
git prunecommand is entirely different. it will delete locally detached commits.
How Do I Clean Outdated Branches?
git fetch --prune is the best utility for cleaning
outdated branches. It will connect to a shared remote repository
remote and fetch all remote branch refs. It will then delete remote
refs that are no longer in use on the remote repository.
Does Git Remote Prune Origin Delete the Local Branch?
git remote prune origin will only delete the refs to
remote branches that no longer exist. Git stores both local and
remote refs. A repository will have
remote/origin ref collections.
git remote prune origin will only prune the refs in
remote/origin. This safely leaves local work in
local/origin. To remove local branches you need to use
git branch -d or replace
Credits: Prune explanation comes from execellent article on Git Prune
Second - delete merged local branches
$ git branch --merged origin/develop | grep -v develop | xargs git branch -d
--merged origin/develop - branches merged into remote
grep -v develop - for safety reasons: exclude
develop from the list of branches to delete
Read more here: https://stackoverflow.com/questions/16590160/remove-branches-not-on-remote