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
In order to clean up remote tracking branches, meaning deleting references to non-existing remote branches, use the “git remote prune” command and specify the remote name. In order to find the name of your current configured remotes, run the “git remote” command with the “-v” option.
Remote Git branch delete command Alternatively, you can issue either of the following Git commands to delete a remote branch: git push origin --delete <branch> git push origin -d <branch>
Deleting Multiple Remote Branches egrep -v "(^\*|master|main)" - exclude branch master and main. sed 's/origin\///' - because the remote branches are prefixed with origin/ this command will filter that part out so it returns only the branch name. xargs -n 1 git push origin --delete - deletes the remaining branches.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With