Created
April 29, 2015 00:00
-
-
Save nyarly/0f169f9e460517d570b2 to your computer and use it in GitHub Desktop.
Two scripts for doing branch matainence.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
should remove all the branches that are done and merged, until someone else pushes them back to origin.