Created
August 15, 2025 13:21
-
-
Save sachin-handiekar/3d1da8377a6c38b71e01f55e69504840 to your computer and use it in GitHub Desktop.
Delete Git Remote branches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script to delete remote Git branches older than 1 year | |
# Usage: ./delete_old_branches.sh [remote_name] [--dry-run] | |
# Default remote is 'origin' | |
set -e | |
# Configuration | |
REMOTE_NAME="${1:-origin}" | |
DRY_RUN=false | |
ONE_YEAR_AGO=$(date -d "1 year ago" +%s) | |
# Check for dry-run flag | |
if [[ "$*" == *"--dry-run"* ]]; then | |
DRY_RUN=true | |
echo "π DRY RUN MODE - No branches will be deleted" | |
fi | |
echo "π Checking for branches older than 1 year on remote '$REMOTE_NAME'..." | |
echo "π Cutoff date: $(date -d "1 year ago" '+%Y-%m-%d')" | |
echo "" | |
# Fetch latest remote information | |
echo "π‘ Fetching latest remote information..." | |
git fetch "$REMOTE_NAME" --prune | |
# Get list of remote branches (excluding HEAD) | |
remote_branches=$(git branch -r | grep "$REMOTE_NAME/" | grep -v "HEAD" | sed "s|$REMOTE_NAME/||" | tr -d ' ') | |
if [ -z "$remote_branches" ]; then | |
echo "β No remote branches found for '$REMOTE_NAME'" | |
exit 1 | |
fi | |
branches_to_delete=() | |
protected_branches=("main" "master" "develop" "development" "staging" "production") | |
echo "π Analyzing branches..." | |
echo "" | |
for branch in $remote_branches; do | |
# Skip protected branches | |
if [[ " ${protected_branches[@]} " =~ " ${branch} " ]]; then | |
echo "π‘οΈ Skipping protected branch: $branch" | |
continue | |
fi | |
# Get the last commit date for the branch | |
last_commit_date=$(git log -1 --format="%ct" "$REMOTE_NAME/$branch" 2>/dev/null || echo "0") | |
if [ "$last_commit_date" -eq 0 ]; then | |
echo "β οΈ Could not get commit date for branch: $branch" | |
continue | |
fi | |
# Check if branch is older than 1 year | |
if [ "$last_commit_date" -lt "$ONE_YEAR_AGO" ]; then | |
last_commit_human=$(date -d "@$last_commit_date" '+%Y-%m-%d') | |
echo "ποΈ Branch '$branch' is old (last commit: $last_commit_human)" | |
branches_to_delete+=("$branch") | |
else | |
last_commit_human=$(date -d "@$last_commit_date" '+%Y-%m-%d') | |
echo "β Branch '$branch' is recent (last commit: $last_commit_human)" | |
fi | |
done | |
echo "" | |
if [ ${#branches_to_delete[@]} -eq 0 ]; then | |
echo "β¨ No old branches found to delete!" | |
exit 0 | |
fi | |
echo "π Summary:" | |
echo " Found ${#branches_to_delete[@]} branch(es) older than 1 year" | |
echo "" | |
if [ "$DRY_RUN" = true ]; then | |
echo "π DRY RUN - Would delete the following branches:" | |
for branch in "${branches_to_delete[@]}"; do | |
echo " - $REMOTE_NAME/$branch" | |
done | |
echo "" | |
echo "π‘ Run without --dry-run to actually delete these branches" | |
exit 0 | |
fi | |
# Confirm deletion | |
echo "β οΈ WARNING: This will permanently delete the following remote branches:" | |
for branch in "${branches_to_delete[@]}"; do | |
echo " - $REMOTE_NAME/$branch" | |
done | |
echo "" | |
read -p "Are you sure you want to continue? (y/N): " -n 1 -r | |
echo "" | |
if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
echo "β Operation cancelled" | |
exit 0 | |
fi | |
echo "" | |
echo "ποΈ Deleting old branches..." | |
deleted_count=0 | |
failed_count=0 | |
# Commented out - only printing list of branches instead of deleting | |
# for branch in "${branches_to_delete[@]}"; do | |
# echo -n " Deleting $REMOTE_NAME/$branch... " | |
# if git push "$REMOTE_NAME" --delete "$branch" 2>/dev/null; then | |
# echo "β Done" | |
# ((deleted_count++)) | |
# else | |
# echo "β Failed" | |
# ((failed_count++)) | |
# fi | |
# done | |
# Just print the list of branches that would be deleted | |
for branch in "${branches_to_delete[@]}"; do | |
echo " π Would delete: $REMOTE_NAME/$branch" | |
((deleted_count++)) | |
done | |
echo "" | |
echo "π Results:" | |
echo " β Successfully deleted: $deleted_count branches" | |
if [ $failed_count -gt 0 ]; then | |
echo " β Failed to delete: $failed_count branches" | |
fi | |
echo "" | |
echo "π Cleanup complete!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment