Is there a command that is able to programatically remove branches older than 1 month and that was merged to master branch in one shot?
Asked
Active
Viewed 1,526 times
0
-
Since when do you count that month? Branch creation (in which case, you won't have that in git itself)? Date of the merge into master? – Romain Valeri Dec 03 '19 at 09:46
-
https://stackoverflow.com/questions/10325599/delete-all-branches-that-are-more-than-x-days-weeks-old – Lazar Nikolic Dec 03 '19 at 09:50
-
If you check the link offered by @LazarNikolic you just need the first part to be "git branch --merged master" instead of simply "git branch". – zrrbite Dec 03 '19 at 10:36
2 Answers
1
Building on top of the answer shared by @LazarNikolic (Delete all branches that are more than X days/weeks old):
for k in $(git branch --merged master | sed /\*/d); do
if [ -n "$(git log -1 --before='1 month ago' -s $k)" ]; then
git branch -D $k
fi
done
Some background:
git branch --merged master
to only list branches that have been merged tomaster
.git log --before
to inspect log entries that are more than 1 month old. If any entries exist, for any merged branch, delete that branch.
0
In my case I'm using git version 2.25.1. The answer above did not work for me but this does.
# Purge branches older than 1 month
for k in $(git branch | sed /\*/d)
do
log "Found branch $k"
if [ -n "$(git log -1 --before='1 month ago' --grep='$k')" ]
then
git push -d origin $k &> /dev/null
git branch -d $k &> /dev/null
log "Purged branch $k"
fi
done

Chrisbot
- 67
- 4