Skip to content

Instantly share code, notes, and snippets.

@nyarly
Created April 29, 2015 00:00
Show Gist options
  • Save nyarly/0f169f9e460517d570b2 to your computer and use it in GitHub Desktop.
Save nyarly/0f169f9e460517d570b2 to your computer and use it in GitHub Desktop.
Two scripts for doing branch matainence.
#!/bin/bash
remote=${1:-origin}
declare -a branches
declare -a local_branches
for branch in $(git branch --merged | egrep -v '(^[*])|(master)|(staging)|(production)|(^\s*$)'); do
branch_remote=$(git config --get branch.$branch.remote)
if [ "$branch_remote" == "$remote" ]; then
branches+=($branch)
fi
if [ -z $branch_remote ]; then
local_branches+=($branch)
fi
done
if [ ${#branches[*]} -ne 0 ]; then
echo Cleaning:
echo ${branches[*]}
logfile=/tmp/$(basename ${0}).log
echo -n > $logfile
for branch in ${branches[*]}; do
#If we can delete the branch remotely, delete it locally
git push origin :$branch 2>> $logfile
git branch -d $branch 2>> $logfile
done
cat $logfile
fi
echo "Did not clean local branches:"
echo ${local_branches[*]}
remotes=$(git remote)
for remote in $remotes; do
git remote prune $remote
done
echo "Running garbage collection"
git gc 2>/dev/null
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $(basename ${0}) <remote>"
exit 2
fi
remote_name=$1
logfile=/tmp/$(basename ${0}).log
echo -n > $logfile
for b in $(git branch -r | grep -v "${remote_name}/HEAD" | grep "\s*${remote_name}/" | sed "s#\s*${remote_name}/##"); do
git branch -t $b "${remote_name}/$b" 2>> $logfile
done
if grep -v "fatal: A branch named" < $logfile; then
exit 1
else
exit 0
fi
@nyarly
Copy link
Author

nyarly commented Apr 29, 2015

git-clean-branches removes all the merged branches in the current workspace that can be deleted on their remote - the idea being that those are probably done.

git-localize-branches creates branches locally to track all the branches on

So:

git-localize-branches origin
git-clean-branches

should remove all the branches that are done and merged, until someone else pushes them back to origin.

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