Created
August 2, 2023 09:37
-
-
Save nickimola/fe80168cdce3af39f831668d550d7dce to your computer and use it in GitHub Desktop.
Simple bash script to list and delete all git branches older than a certain amount of days. Default to 365 days (1 year)
This file contains 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 | |
DAYS=365 | |
RED='\033[0;31m' | |
BLUE='\033[0;34m' | |
GREEN='\033[0;32m' | |
NC='\033[0m' # No Color | |
git checkout development | |
tarBranch=$(git branch -r --no-merged | grep -v master | grep -v developer | grep -v release | sed 's/origin\///') | |
for branch in $tarBranch | |
do | |
lastDate=$(git show -s --format=%ci origin/$branch) | |
convertDate=$(echo $lastDate | cut -d' ' -f 1 -f 2) | |
Todate=`date -ju -f "%F %T" "$convertDate" "+%s"` | |
current=$(date +'%s') | |
day=$(( ( $current - $Todate )/60/60/24 )) | |
if [ "$day" -gt $DAYS ]; then | |
echo "last commit on ${BLUE}$branch${NC} was ${RED}$day${NC} days ago ====> ${RED}DELETE${NC}" | |
git push origin :$branch | |
else | |
echo "last commit on ${BLUE}$branch${NC} was ${GREEN}$day${NC} days ago ====> ${GREEN}SKIP${NC}" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment