Skip to content

Instantly share code, notes, and snippets.

@shayan09
Last active September 21, 2021 22:40
Show Gist options
  • Save shayan09/c32c32edda1d63a037bc42a8e46693a6 to your computer and use it in GitHub Desktop.
Save shayan09/c32c32edda1d63a037bc42a8e46693a6 to your computer and use it in GitHub Desktop.
Delete old git branches locally and remote
#!/bin/bash
ECHO='echo '
for branch in $(git branch -a | sed 's/^\s*//' | sed 's/^remotes\///' | grep -v 'master\|develop\|main'); do
if ! ( [[ -f "$branch" ]] || [[ -d "$branch" ]] ) && [[ "$(git log $branch --since "3 months ago" | wc -l)" -eq 0 ]]; then
local_branch_name=${branch/'origin/'/''}
git branch -d "${local_branch_name}"
git push origin --delete "${local_branch_name}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment