Created
April 13, 2022 20:08
-
-
Save elusive/c68ab4bffd17342a300a6f1b0b0013e9 to your computer and use it in GitHub Desktop.
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 | |
### Copy of https://gist.github.com/redthor/3776c1f726cafff41c9eda6f271466c3 | |
### Adding in comments explaining each line/command in order to help learning | |
### Set DRY_RUN=1 to only echo the commands instead of executing them | |
#DRY_RUN=1 | |
# Date parameter uses git log --since syntax | |
# i.e. "2 months ago" or just a simple date | |
date=$1 | |
branches= # store results of our filtered branch list to prevent | |
# multiple calls to remote to get each branch again | |
for branch in $(git branch -a | # call git for list of branches | |
sed 's/^\s*//' | # pipe list of branches to remove any whitespace | |
sed 's/^remotes\///' | # pipe list to remove remote/ from each branch | |
grep -v 'main$' | # pipe to grep to exclude main | |
grep -v 'develop$'); do # pipe to grep to exclude develop | |
# Set of conditionals executed for each branch | |
if [[ "$(git log $branch --since $date | wc -l)" -eq 0 ]]; then # does branch log since date have wc > 0 | |
if [[ "$branch" =~ "origin/" ]]; then # does branch start with origin/ | |
if [[ -z $branches ]]; then # if $branches is empty | |
branches=$(echo "$branch" | sed 's/^origin\///') # remove 'origin/' from $branch and add to branches | |
else | |
branches="$branches "$(echo "$branch" | sed 's/^origin\///') # add to branches | |
fi | |
fi | |
fi | |
done | |
if [[ ! -z $branches ]]; then # if $branches is not empty | |
if [[ "$DRY_RUN" -eq 1 ]]; then # if DRY_RUN=1 then we just echo commands | |
echo git branch -D $branches | |
echo git push --delete origin $branches | |
else # else we execute the commands to remove | |
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