clear-branch.sh 891 Bytes
#!/bin/bash

currentTime=1581570945
sys=$(uname)
all_branches=$(git branch -r | grep -v HEAD | grep -v main | grep -v test | grep -v master | grep -v masterSplit | grep -v bugFix)
if [ $sys = "Darwin" ]; then
  currentTime=$(date -v-60d "+%s")
fi

for br1 in $all_branches; do
  br1_simple_name=${br1#origin/*}
  commit_ts=$(git log $br1 -1 --format="%ct")
  if [[ "" != "$br1" ]] && [[ $commit_ts -lt $currentTime ]]; then
    if [ $sys = "Darwin" ]; then
      commit_time=$(date -r${commit_ts} +"%Y-%m-%d %H:%M")
    else
      commit_time=$(date -d @${commit_ts} +"%Y-%m-%d %H:%M")
    fi
    read -p " Are you sure delete the branch ** $br1 | $commit_time ** (y/n)[n]: " answer
    if [[ "$answer" = "Y" || "${answer}" = "y" ]]; then
      echo " Yes! deleting branch $br1"
      git push origin --delete $br1_simple_name
    else
      echo " Skipped!!!"
    fi
  fi

done
echo "over"