Skip to content

Instantly share code, notes, and snippets.

@antony-scott
Created April 28, 2025 10:04
Show Gist options
  • Save antony-scott/f69105ec1e56b02cfab861ff048300cf to your computer and use it in GitHub Desktop.
Save antony-scott/f69105ec1e56b02cfab861ff048300cf to your computer and use it in GitHub Desktop.
git-prune: remove all remotely deleted branches & get latest master/main
function git-prune() {
# Determine the default branch (main or master)
local branch
branch=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')
# Fallback if detection fails
if [ -z "$branch" ]; then
branch="master"
fi
git checkout "$branch"
git fetch -p
git pull
git branch -vv | awk '/: gone]/{print $1}' | xargs -r git branch -d -f
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment