Skip to content

Instantly share code, notes, and snippets.

@nekonado
Created November 29, 2023 14:13
Show Gist options
  • Save nekonado/5c9c1c3fc16acf96fe7f26c827c7cfc6 to your computer and use it in GitHub Desktop.
Save nekonado/5c9c1c3fc16acf96fe7f26c827c7cfc6 to your computer and use it in GitHub Desktop.
#!/bin/zsh
# Fetch changes from the remote repository and prune (delete) remote tracking branches that no longer exist on the remote
git fetch --prune && \
# Display a list of local branches and their associated remote branches
git branch -vv | \
# Extract lines related to deleted remote branches
grep ': gone]' | \
# Extract branch names from the lines that were extracted
awk '{print $1}' | \
# Receive branch names and delete the corresponding local branches; the -r option prevents the command from running if there are no arguments
xargs -r git branch -d
@nekonado
Copy link
Author

one-liner

git fetch --prune && git branch -vv | grep ': gone]' | awk '{print $1}' | xargs -r git branch -d

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