Skip to content

Instantly share code, notes, and snippets.

@jasonrhodes
Forked from bjorng/git-clean-stale-branches
Last active October 24, 2018 14:23
Show Gist options
  • Save jasonrhodes/80af7903da96c58e917bc89c8860830b to your computer and use it in GitHub Desktop.
Save jasonrhodes/80af7903da96c58e917bc89c8860830b 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
# Prune (remove) locally tracked remote branches that no longer exist in the remote
git fetch $remote --prune
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment