Skip to content

Instantly share code, notes, and snippets.

@cnak
Created September 10, 2021 10:21
Show Gist options
  • Save cnak/358c9a6c67f6d8e3fa67b0b229593252 to your computer and use it in GitHub Desktop.
Save cnak/358c9a6c67f6d8e3fa67b0b229593252 to your computer and use it in GitHub Desktop.
A bash script to prune stale branches that older than 3 months
#!/usr/bin/env bash
echo ====================================
echo "Pulling the latest branches"
git pull
echo "\n"
for branch in `git branch -r | grep -v HEAD`; do \
echo -e `git show -s --format="%cr %an" $branch | head -n 1` \\t$branch; \
done | \
grep -e '.. months' -e '[3-9] months' | grep 'origin/' | \
sed 's/.*origin\///' &&
echo "\nYou sure you want to delete these?"
echo "y or n"
read answer
if [ "$answer" == "y" ] || [ "$answer" = "yes" ]
then
echo "Deleting STALE remote branches"
for branch in `git branch -r | grep -v HEAD`; do \
echo -e `git show --format="%cr %an" $branch | head -n 1` \\t$branch; \
done | \
grep -e '.. months' -e '[3-9] months' | grep 'origin/' | \
sed 's/.*origin\///' | \
xargs -n 1 echo git push origin --delete | bash
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment