Skip to content

Instantly share code, notes, and snippets.

@walkerdepaula
Created October 1, 2020 17:57
Show Gist options
  • Save walkerdepaula/b4c092b2e1056363cb37ba171a4db443 to your computer and use it in GitHub Desktop.
Save walkerdepaula/b4c092b2e1056363cb37ba171a4db443 to your computer and use it in GitHub Desktop.
[GIT] Prune local tracking branches that do not exist on remote anymore

(requires bash or zsh, won't work with standard Bourne shell)

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.

1:

$ git fetch --prune

2:

$ git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment