Last active
September 15, 2020 12:49
-
-
Save redthor/3776c1f726cafff41c9eda6f271466c3 to your computer and use it in GitHub Desktop.
Script to delete branches older than a certain date, modification of 4586456
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
# Copy of https://gist.github.com/antonio/4586456 | |
# With a modification to collect all the branch names so we can make one git request | |
# Set DRY_RUN=1 to get an echo of the command | |
# Format that works with `git log --since`, e.g. 2018-01-01 | |
date=$1 | |
branches= | |
for branch in $(git branch -a | sed 's/^\s*//' | sed 's/^remotes\///' | grep -v 'master$'); do | |
if [[ "$(git log $branch --since $date | wc -l)" -eq 0 ]]; then | |
if [[ "$branch" =~ "origin/" ]]; then | |
if [[ -z $branches ]]; then | |
branches=$(echo "$branch" | sed 's/^origin\///') | |
else | |
branches="$branches "$(echo "$branch" | sed 's/^origin\///') | |
fi | |
fi | |
fi | |
done | |
if [[ ! -z $branches ]]; then | |
if [[ "$DRY_RUN" -eq 1 ]]; then | |
echo git branch -D $branches | |
echo git push --delete origin $branches | |
else | |
git branch -D $branches | |
git push --delete origin $branches | |
# clean up locally | |
git remote prune origin | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
do we need to fetch all the branches to the local machine where we are running the above script? When I used the above script I see the delete branch command is failing with an error: branch '$branchname' not found.