Skip to content

Instantly share code, notes, and snippets.

@ducksoupdev
Created March 22, 2023 14:42
Show Gist options
  • Save ducksoupdev/778177ec141b40a037f62263917e9e46 to your computer and use it in GitHub Desktop.
Save ducksoupdev/778177ec141b40a037f62263917e9e46 to your computer and use it in GitHub Desktop.
Delete old branches
#!/bin/bash
# Set the age threshold in seconds (7 days)
age=$((7 * 24 * 60 * 60))
# Loop through all local branches
for branch in $(git for-each-ref --format='%(refname:short)' refs/heads/); do
# Get the timestamp of the last commit on the branch
timestamp=$(git log -1 --format="%ct" "$branch")
# Calculate the age of the branch in seconds
age_branch=$(( $(date +%s) - $timestamp ))
# Check if the branch is older than the threshold
if [ $age_branch -gt $age ]; then
# Delete the branch
git branch -D "$branch"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment