Skip to content

Instantly share code, notes, and snippets.

@shayan09
shayan09 / delete-old-branches.sh
Last active September 21, 2021 22:40
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