Skip to content

Instantly share code, notes, and snippets.

@aausch
Created February 24, 2025 20:26
Show Gist options
  • Save aausch/bfccc39bf32c452e5fbca4d83e984356 to your computer and use it in GitHub Desktop.
Save aausch/bfccc39bf32c452e5fbca4d83e984356 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Adapted to work with OSX as of Feb 2025
# Fetch the latest state of remote branches and prune deleted branches
git fetch --prune
# brew install coreutils for `gdate`
# Get the current date minus 365 days
THRESHOLD_DATE=$(gdate -d '365 days ago' +%s)
# Loop through remote branches
for branch in $(git branch -r | grep -v '\->'); do
# Get the last commit date of the branch
LAST_COMMIT_DATE=$(git log -1 --format=%ct "${branch}")
# Check if the branch is older than the threshold
if [[ $LAST_COMMIT_DATE -lt $THRESHOLD_DATE ]]; then
# Delete the remote branch if it's older than 365 days
echo "Deleting branch: ${branch#origin\/}" # Inform about the deletion
git push origin --delete "${branch#origin\/}" # Delete the branch
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment