Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dee-me-tree-or-love/ffc27e1c6e5621f837283afb2ce5852c to your computer and use it in GitHub Desktop.

Select an option

Save dee-me-tree-or-love/ffc27e1c6e5621f837283afb2ce5852c to your computer and use it in GitHub Desktop.
Check the active branches on remote, clean up the local branches that are no longer existing on remote
#!/bin/sh
################################################
# cleanup gone remote branches from local list #
################################################
git fetch -pP \ # fetch and prune gone remotes and remote tags
&& git branch -vv \ # display information about all branches
| grep "\: gone\]" \ # find all the branches with "gone" remotes
| awk '{print $1}' \ # get the branch names
| xargs -i{} git branch -D {} # delete the local branches that are gone on remote
##################
# as a one liner #
##################
# ~
# $ git fetch -pP && git branch -vv | grep "\: gone\]" | awk '{print $1}' | xargs -i{} git branch -D {}
# ~
##################
# as a git alias #
##################
# ~
# $ git config --global alias.synclocalbranches \
# '!f() { git fetch -pP && git branch -vv | grep "\: gone\]" | awk "{print $1}" | xargs -i{} git branch -D {}; }; f'
# ~
# This alias can now be run with
# ~
# $ git synclocalbranches
# ~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment