Skip to content

Instantly share code, notes, and snippets.

@bjorng
Created July 25, 2009 09:48
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save bjorng/154761 to your computer and use it in GitHub Desktop.
Save bjorng/154761 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Suggested name for this script: git-clean-stale-branches
#
# This script will help to remove "stale" branches from a remote
# repository (by default the "origin" repository). Stale branches
# are any branches that does not exist in the local repository.
#
# This script should be run in the local repository. It will print
# out a git command to remove all branches from the remote repository
# that do not exist locally. If the command seems to be correct,
# you can copy and paste the command and run it, or re-run the script
# like this:
#
# git-clean-stale-branches | sh
#
IFS='
'
# Name of remote repository. Can be edited.
remote=origin
printf "git push $remote"
for i in `git branch -r | grep "^ *$remote/" | grep -v HEAD | sed "s;^ *$remote/;;"`
do
if git rev-parse -q --verify $i >/dev/null
then
nothing=
else
printf " :%s" "$i"
fi
done
printf "\n"
@jasonrhodes
Copy link

Thanks for this! To prevent problems where you have locally tracked remote branches that no longer exist in the remote, I added this at line 22 right before you start in the for loop:

git fetch $remote --prune

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