Skip to content

Instantly share code, notes, and snippets.

@akhileshdarjee
Created August 31, 2022 14:02
Show Gist options
  • Save akhileshdarjee/e84aa26fd35afd9387ee1b8f6da43d86 to your computer and use it in GitHub Desktop.
Save akhileshdarjee/e84aa26fd35afd9387ee1b8f6da43d86 to your computer and use it in GitHub Desktop.
Delete/Remove old Git Branches
#!/bin/bash
##
# Script to delete remote git branches
##
# Fetch the remote resources
git fetch
# Loop through all remote merged branches
count=0
date='Sep 1, 2021'
echo "Branches that are older than $date"
for branch in $(git branch -r --merged | grep -v HEAD | grep -v develop | grep -v master | grep -v master | sed /\*/d); do
if [ -z "$(git log -1 --since="$date" -s ${branch})" ]; then
echo -e `git show --format="%ci %cr %an" ${branch} | head -n 1` \\t$branch
count=$((count + 1))
remote_branch=$(echo ${branch} | sed 's#origin/##' )
# To delete the branches uncomment the bellow git delete command
#git push origin --delete ${remote_branch}
fi
done
echo "Found $count merged branches that have no commits since $date"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment