Created
January 21, 2013 14:29
-
-
Save antonio/4586456 to your computer and use it in GitHub Desktop.
Script to delete branches older than a certain date
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/sh | |
date=$1 | |
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 | |
local_branch_name=$(echo "$branch" | sed 's/^origin\///') | |
if [[ "$DRY_RUN" -eq 1 ]]; then | |
echo "git push origin :$local_branch_name" | |
else | |
git push origin :$local_branch_name | |
fi | |
else | |
if [[ "$DRY_RUN" -eq 1 ]]; then | |
echo "git branch -D $branch" | |
else | |
git branch -D $branch | |
fi | |
fi | |
fi | |
done |
It uses the git since syntax. Here is what I found in the examples section of the man log
git log --since="2 weeks ago" -- gitk
Show the changes during the last two weeks to the file gitk. The "--" is necessary to avoid confusion with the branch named gitk
I've created an alternative that will make (I think) one git request to the remote instead of one for each branch:
https://gist.github.com/redthor/3776c1f726cafff41c9eda6f271466c3
Also the date format that works is like "2018-01-01" (using "2 weeks ago" with spaces will fail)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how is the date format?