delete-remote-branches.sh
1 #!/bin/sh 2 # Delete all remote branches that don't have a local copy. 3 4 remote="rad" 5 remote_branches="$(git for-each-ref --format='%(refname:short)' refs/remotes/rad)" 6 7 # Check that a branch isn't a remote tracking branch. 8 is_remote_branch() { 9 for remote_branch in $remote_branches; do 10 # Remove the "rad/" prefix 11 if [ "$1" = "${remote_branch#rad/}" ]; then 12 return 0 13 fi 14 done 15 return 1 16 } 17 18 # Iterate over all remote branches. 19 for branch in $(git branch -r --format "%(refname:short)"); do 20 # Extract the branch name without the "$remote/" prefix. 21 branch=${branch#"$remote/"} 22 # Never delete the master branch. 23 if [ "$branch" = "master" ]; then 24 continue 25 fi 26 27 # Check if the branch doesn't exist locally. 28 if ! git rev-parse --quiet --verify "$branch" >/dev/null; then 29 if ! is_remote_branch "$branch"; then 30 git push -o "no-sync" $remote --delete "$branch" 31 echo "Deleted '$branch'" 32 else 33 echo "Skipping remote branch '$branch'" 34 fi 35 fi 36 done 37 38 rad sync --announce