-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_old_branches.sh
More file actions
33 lines (25 loc) · 945 Bytes
/
delete_old_branches.sh
File metadata and controls
33 lines (25 loc) · 945 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env bash
# Delete branches older than 365 days
THRESHOLD_DAYS=365
# Use a pipe delimiter to separate fields clearly
git for-each-ref --format='%(committerdate:iso8601)|%(refname:short)' refs/remotes/origin/ | while IFS='|' read -r date branch; do
# Sanitize branch name
if [[ "$branch" == "origin/HEAD" ]]; then
continue
fi
branch_name="${branch#origin/}"
# Convert date string to epoch
branch_date_sec=$(date -d "$date" +%s 2>/dev/null)
now_sec=$(date +%s)
if [[ -z "$branch_date_sec" ]]; then
echo "⚠️ Skipping invalid date for branch: $branch_name"
continue
fi
age_days=$(( (now_sec - branch_date_sec) / 86400 ))
if (( age_days > THRESHOLD_DAYS )); then
echo "🗑 Deleting remote branch: $branch_name (last commit: $date, $age_days days old)"
git push origin --delete "$branch_name"
else
echo "✅ Keeping $branch_name (last commit: $date, $age_days days old)"
fi
done