Alex Alex - 1 month ago 4x
Git Question

How to prune local tracking branches that do not exist on remote anymore


git remote prune origin
I can remove the remote branches that are not any more on the remote.

But I want to remove also those local branches that were created from those remote branches (a check if they are unmerged would be nice).

How can I do this?


After pruning, you can get the list of remote branches with git branch -r. The list of branches with their remote tracking branch can be retrieved with git branch -vv. So using these two lists you can find the remote tracking branches that are not in the list of remotes.

This line should do the trick:

git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d

This string gets the list of remote branches and passes it into egrep through the standard input. And filters the branches that have a remote tracking branch (using git branch -vv and filtering for those that have origin) then getting the first column of that output which will be the branch name. Finally passing all the branch names into the delete branch command.

Since it is using the -d option, it will not delete branches that have not been merged into the branch that you are on when you run this command.

Edit: for those interested in a single command to do the entire job, bear in mind that first you need to prune, otherwise, the command above will not work.


git fetch --prune && git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d