Skip to content

Instantly share code, notes, and snippets.

@imran-baig-se
Created April 12, 2017 08:13
Show Gist options
  • Save imran-baig-se/d0546e42d9bd85a964a3d9cbcaa85922 to your computer and use it in GitHub Desktop.
Save imran-baig-se/d0546e42d9bd85a964a3d9cbcaa85922 to your computer and use it in GitHub Desktop.
Clean git branches by specifying months
#!/usr/bin/env bash
# fetch all branches:
for k in $(git branch -a | sed /\*/d); do
# check if there is no changes since 3 month
if [ -z "$(git log -1 --since='3 month ago' -s $k)" ]; then
# prepare branch name
branch_name_with_no_origin=$(echo $k | sed -e "s/origin\///" | sed -e "s/remotes\///" )
echo deleting branch: $branch_name_with_no_origin
# keep master | develop branch even if they are outdated.
if [ "$branch_name_with_no_origin" = "master" ] || [ "$branch_name_with_no_origin" = "develop" ]; then
echo "skip $branch_name_with_no_origin"
else
echo "deleting branch $branch_name_with_no_origin"
git push origin --delete $branch_name_with_no_origin
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment