Created
August 31, 2022 14:02
-
-
Save akhileshdarjee/e84aa26fd35afd9387ee1b8f6da43d86 to your computer and use it in GitHub Desktop.
Delete/Remove old Git Branches
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 | |
## | |
# 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