Skip to content

Instantly share code, notes, and snippets.

@frederikstroem
Created September 30, 2024 08:11
Show Gist options
  • Save frederikstroem/591654278b1f5cb9c15e069db7213ccd to your computer and use it in GitHub Desktop.
Save frederikstroem/591654278b1f5cb9c15e069db7213ccd to your computer and use it in GitHub Desktop.
Bash alias to list or prune local tracking branches that do not exist on remote anymore.
# Source: https://web.archive.org/web/20240930075945/https%3A%2F%2Fstackoverflow.com%2Fquestions%2F13064613%2Fhow-to-prune-local-tracking-branches-that-do-not-exist-on-remote-anymore#17029936
alias git-list-untracked='git fetch --prune && git branch -r | awk "{print \$1}" | grep -E -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk "{print \$1}"'
alias git-remove-untracked='git fetch --prune && git branch -r | awk "{print \$1}" | grep -E -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk "{print \$1}" | xargs git branch -d'
alias git-remove-untracked-force-unmerged='git fetch --prune && git branch -r | awk "{print \$1}" | grep -E -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk "{print \$1}" | xargs git branch -D'
@frederikstroem
Copy link
Author

git-remove-untracked-force-unmerged has been added and egrep has been replaced with grep -E.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment