Created
March 22, 2023 14:42
-
-
Save ducksoupdev/778177ec141b40a037f62263917e9e46 to your computer and use it in GitHub Desktop.
Delete old 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 | |
# 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