Last active
May 24, 2019 23:27
-
-
Save mstifflin/07074cdaca646e0a1adc9c3c38d9cd5b to your computer and use it in GitHub Desktop.
Delete remote git branches older than 3 week old, git remote branch clean up
This file contains hidden or 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
# Deletes all remote branches 3 weeks or older | |
delbr() { | |
delbrtest | |
echo "Continue? (y/n)" | |
read shouldContinue | |
if [ $shouldContinue = "n" ]; then | |
echo "Aborting" | |
return 0 | |
fi | |
echo Running remote branch clean up... | |
echo Deleting all branches 3 weeks and older | |
prefix="origin/" | |
for k in $(git branch -r | sed /\*/d); do | |
if [ $k = "->" ] || [ $k = "origin/master" ] || [ $k = "origin/HEAD" ]; then | |
continue | |
fi | |
if [ -z "$(git log -1 --since='3 weeks ago' -s $k)" ]; then | |
echo Deleting "$(git log -1 --pretty=format:"%ct" $k) $k"; | |
git push origin :${k#$prefix} | |
fi | |
done | |
} | |
delbrtest() { | |
echo Running remote branch clean up test... | |
echo Printing all branches that will be deleted | |
echo Will delete all branches 3 weeks and older | |
prefix="origin/" | |
for k in $(git branch -r | sed /\*/d); do | |
if [ $k = "->" ] || [ $k = "origin/master" ] || [ $k = "origin/HEAD" ]; then | |
continue | |
fi | |
if [ -z "$(git log -1 --since='3 weeks ago' -s $k)" ]; then | |
echo "COMMAND will be run: git push origin :${k#$prefix}" | |
else | |
echo "NOT deleted ${k#$prefix}" | |
fi | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment