Skip to content

Instantly share code, notes, and snippets.

@dominicgan
Created November 2, 2023 03:24
Show Gist options
  • Save dominicgan/bd15fa0f8cc2f1897e8e2c757d0e3d20 to your computer and use it in GitHub Desktop.
Save dominicgan/bd15fa0f8cc2f1897e8e2c757d0e3d20 to your computer and use it in GitHub Desktop.
Find and delete all branches more than 60 days
reference_date=$(date -v-60d "+%s")
branches_to_delete=()
git for-each-ref --format='%(committerdate:raw)%09%(refname:short)' | while read date branch; do
commit_date=$(echo "$date" | cut -d' ' -f1)
if [ "$commit_date" -lt "$reference_date" ]; then
branches_to_delete+=("$branch")
echo "$branch has not been updated for more than 60 days"
fi
done
if [ ${#branches_to_delete[@]} -eq 0 ]; then
echo "No branches found that haven't been updated for more than 60 days."
else
read -p "Do you want to delete these branches? (y/n): " answer
if [ "$answer" = "y" ]; then
for branch in "${branches_to_delete[@]}"; do
git branch -D "$branch"
echo "Branch '$branch' deleted."
done
else
echo "Branch deletion cancelled."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment