Reputation: 10725
I have a LOT of GIT branches on my "remote" server.
This answer is quite nice, but it doesn't get me all the way there. How can I delete all Git branches which have been merged?
Can you please include master/develop branches from the merge? How do I add a time interval on this?
git branch -r --merged | grep -v master | sed 's/origin\///' | xargs -n 1 git push --delete origin
Upvotes: 8
Views: 6237
Reputation: 38106
You can use shell script to delete no merged branches which are older then one year, and delete the merged branches which are older than five months.
#!/bin/bash
tarBranch=$(git branch -r --no-merged | grep -v master | grep -v developer | sed 's/origin\///')
for branch in $tarBranch
do
echo $branch
lastDate=$(git show -s --format=%ci origin/$branch)
convertDate=$(echo $lastDate | cut -d' ' -f 1)
Todate=$(date -d "$convertDate" +'%s')
current=$(date +'%s')
day=$(( ( $current - $Todate )/60/60/24 ))
echo "last commit on $branch branch was $day days ago"
if [ "$day" -gt 365 ]; then
git push origin :$branch
echo "delete the old branch $branch"
fi
done
#!/bin/bash
git checkout master
#deleted merged branches on master branch
tarBranch=$(git branch -r --merged | grep -v master | grep -v develop | sed 's/origin\///')
for branch in $tarBranch
do
echo $branch
lastDate=$(git show -s --format=%ci origin/$branch)
convertDate=$(echo $lastDate | cut -d' ' -f 1)
Todate=$(date -d "$convertDate" +'%s')
current=$(date +'%s')
day=$(( ( $current - $Todate )/60/60/24 ))
echo "last commit on $branch branch was $day days ago"
if [ "$day" -gt 150 ]; then
git push origin :$branch
echo "delete the old branch $branch"
fi
done
git checkout develop
#deleted merged branches on developer branch
tarBranch=$(git branch -r --merged | grep -v master | grep -v develop | sed 's/origin\///')
for branch in $tarBranch
do
echo $branch
lastDate=$(git show -s --format=%ci origin/$branch)
convertDate=$(echo $lastDate | cut -d' ' -f 1)
Todate=$(date -d "$convertDate" +'%s')
current=$(date +'%s')
day=$(( ( $current - $Todate )/60/60/24 ))
echo "last commit on $branch branch was $day days ago"
if [ "$day" -gt 150 ]; then
git push origin :$branch
echo "delete the old branch $branch"
fi
done
Upvotes: 6