Skip to content

Instantly share code, notes, and snippets.

@mstifflin
Last active May 24, 2019 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstifflin/07074cdaca646e0a1adc9c3c38d9cd5b to your computer and use it in GitHub Desktop.
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
# 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