Skip to content

Instantly share code, notes, and snippets.

@karlkaebnick
Created January 9, 2020 18:40
Show Gist options
  • Save karlkaebnick/259f967e30a3dab93f2b316b85b7c037 to your computer and use it in GitHub Desktop.
Save karlkaebnick/259f967e30a3dab93f2b316b85b7c037 to your computer and use it in GitHub Desktop.
Shell script to delete old git branches
#!/usr/bin/env bash

# Run the following 2 lines by hand - edit each output file to make sure there is nothing you want to keep

# git branch --all --sort=committerdate --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))' > branch_info.txt

# cat branch_info.txt | sed -e "s/^  origin\///" -e "s/ .*//" > delete_me.txt


while read -r branch;
do

    echo "deleting branch ${branch}"
    # echo "git push origin :${branch}"

    git push origin ":${branch}"
    retval=$?

    if [ ${retval} != 0 ]
    then

        echo "Error ${retval} deleting branch ${branch}"
        exit ${retval}

    fi

    sleep 5

done < delete_me.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment