Skip to main content

Remove git branches older than X months

1 min read

For the example, we're deleting merged branches older than 3 months :

for k in $(git branch --sort=committerdate --remote --merged | grep -o -P "(?<=origin\/)[^ ]+"); do
  if [[ ! $(git log -1 --since='3 month ago' -s origin/$k) ]]; then
    git push --delete origin $k
  fi
done