Skip to content

Instantly share code, notes, and snippets.

@d3vAdv3ntur3s
Created January 13, 2022 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d3vAdv3ntur3s/e331550e1b0d49d8803b3be7b45a0549 to your computer and use it in GitHub Desktop.
Save d3vAdv3ntur3s/e331550e1b0d49d8803b3be7b45a0549 to your computer and use it in GitHub Desktop.
Delete local & remote git branches after x months old
#!/bin/sh
for branch in $(git branch -a | sed 's/^\s*//' | sed 's/^remotes\///' | grep -v 'master$\|develop$'); do
if ! ( [[ -f "$branch" ]] || [[ -d "$branch" ]] ) && [[ "$(git log $branch --since "6 month ago" | wc -l)" -eq 0 ]]; then
local_branch_name=$(echo "$branch" | sed 's/remotes\/origin\///')
git branch -d "${local_branch_name}"
echo "${local_branch_name}"
git push origin --delete "${local_branch_name}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment