Skip to content

Instantly share code, notes, and snippets.

@nvie
Created December 3, 2012 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nvie/4194167 to your computer and use it in GitHub Desktop.
Save nvie/4194167 to your computer and use it in GitHub Desktop.
Very rough script that cleans up all old branches that are loosely hanging around after a while. Also updates the origin remote.
#!/bin/sh
fix_branch_output () {
# Strips first columns from the output, to remove whitespace and "current
# branch" marker from git-branch's output
cut -c 3-
}
local_branches () {
git branch | fix_branch_output
}
remote_branches () {
git branch -r | grep -v ' -> ' | fix_branch_output
}
all_branches () {
local_branches
remote_branches
}
filter_nontrunks () {
#grep -vEe '(^|/)(master|develop|trunk|production)$'
grep -vEe '^(origin/)?master$'
}
branches () {
all_branches | filter_nontrunks
}
to_sha () {
git show | awk "NR==1 {print \$2}"
}
startswith() { [ "$1" != "${1#$2}" ]; }
base_master=$(git merge-base "master" "origin/master")
for branch in `branches`; do
echo "$branch... \c"
#if is_ancestor "$branch" master; then
branch_sha=$(git rev-parse "$branch")
base=$(git merge-base "$branch" "$base_master")
if [ "$base" == "$branch_sha" -a "$base" != "$base_master" ]; then
echo "OK"
git branch -d "$branch"
if startswith "$branch" origin/; then
git push origin --delete "${branch#origin/}"
fi
else
echo "SKIP"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment