Git-ting my branches up to date
Once upon a time a developer created a huge number, as far as more than five is considered huge, of feature branches; each of them based on a master branch and there wasn't any significance to the actual timestamps of commits. "Soon", he vowed, "... soon, I shall integrate all features! Soon but not today." So he created a short script to update all branches to the latest and greatest of the master branch, adding some color for his own pleasure and gracefully failing the merges in case of any errors. The following piece of code was the result.
bash
#!/bin/sh# Lets define some nice colourstxtbld=$(tput bold)txtrst=$(tput sgr0)txtred=$(tput setaf 1)txtwht=$(tput setaf 7)txtblu=$(tput setaf 4)# maybe I want a list of all failed updatesfails=""git for-each-ref 'refs/heads/\*' | \while read rev type ref; dobranch=$(expr "$ref" : 'refs/heads/\(.\*\)' )revs=$(git rev-list $rev..master)if [ -n "$revs" ]; then# Ok, so this branch isnt up to date, mention itecho -e "${txtbld}$branch needs update${txtrst}"git rebase master $branchif [ -d "`git rev-parse --git-dir`/rebase-apply" ]; thengit rebase --abort # fail gracefully, so basically abort and mention itecho -e "${txtred}rebase aborted for branch ${txtblu}${txtbld}$branch${txtrst}"fails="$fails $branch"fifidone
bash
#!/bin/sh# Lets define some nice colourstxtbld=$(tput bold)txtrst=$(tput sgr0)txtred=$(tput setaf 1)txtwht=$(tput setaf 7)txtblu=$(tput setaf 4)# maybe I want a list of all failed updatesfails=""git for-each-ref 'refs/heads/\*' | \while read rev type ref; dobranch=$(expr "$ref" : 'refs/heads/\(.\*\)' )revs=$(git rev-list $rev..master)if [ -n "$revs" ]; then# Ok, so this branch isnt up to date, mention itecho -e "${txtbld}$branch needs update${txtrst}"git rebase master $branchif [ -d "`git rev-parse --git-dir`/rebase-apply" ]; thengit rebase --abort # fail gracefully, so basically abort and mention itecho -e "${txtred}rebase aborted for branch ${txtblu}${txtbld}$branch${txtrst}"fails="$fails $branch"fifidone
After months this turned out to be a very useful script. It saved him from tediously updating each feature branch he had and he never forgot to update them all.