Skip to content

Instantly share code, notes, and snippets.

View shayan09's full-sized avatar
:electron:

Shayan shayan09

:electron:
  • USA
  • 16:33 (UTC -06:00)
View GitHub Profile
@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