Skip to content

Instantly share code, notes, and snippets.

@d4rkd0s
Forked from njames/delete_branches_older_than.sh
Last active August 31, 2021 18:43
Show Gist options
  • Save d4rkd0s/f8d54a620af9d8d1e519108d2bedc84f to your computer and use it in GitHub Desktop.
Save d4rkd0s/f8d54a620af9d8d1e519108d2bedc84f to your computer and use it in GitHub Desktop.
Script to delete branches older than 6 months old, ignore local vs remote errors.
#!/bin/sh
ECHO='echo '
for branch in $(git branch -a | sed 's/^\s*//' | sed 's/^remotes\///' | grep -v 'master$\|release$'); do
if ! ( [[ -f "$branch" ]] || [[ -d "$branch" ]] ) && [[ "$(git log $branch --since "6 months ago" | wc -l)" -eq 0 ]]; then
if [[ "$DRY_RUN" = "false" ]]; then
ECHO=""
fi
local_branch_name=$(echo "$branch" | sed 's/remotes\/origin\///')
$ECHO git branch -d "${local_branch_name}"
$ECHO git push origin --delete "${local_branch_name}"
fi
done
@d4rkd0s
Copy link
Author

d4rkd0s commented Aug 31, 2021

Forked for modification and usage at SR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment