Skip to main content
Glimpses of Daniel's world

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 colours
txtbld=$(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 updates
fails=""
git for-each-ref 'refs/heads/\*' | \
while read rev type ref; do
branch=$(expr "$ref" : 'refs/heads/\(.\*\)' )
revs=$(git rev-list $rev..master)
if [ -n "$revs" ]; then
# Ok, so this branch isnt up to date, mention it
echo -e "${txtbld}$branch needs update${txtrst}"
git rebase master $branch
if [ -d "`git rev-parse --git-dir`/rebase-apply" ]; then
git rebase --abort # fail gracefully, so basically abort and mention it
echo -e "${txtred}rebase aborted for branch ${txtblu}${txtbld}$branch${txtrst}"
fails="$fails $branch"
fi
fi
done
bash
#!/bin/sh
# Lets define some nice colours
txtbld=$(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 updates
fails=""
git for-each-ref 'refs/heads/\*' | \
while read rev type ref; do
branch=$(expr "$ref" : 'refs/heads/\(.\*\)' )
revs=$(git rev-list $rev..master)
if [ -n "$revs" ]; then
# Ok, so this branch isnt up to date, mention it
echo -e "${txtbld}$branch needs update${txtrst}"
git rebase master $branch
if [ -d "`git rev-parse --git-dir`/rebase-apply" ]; then
git rebase --abort # fail gracefully, so basically abort and mention it
echo -e "${txtred}rebase aborted for branch ${txtblu}${txtbld}$branch${txtrst}"
fails="$fails $branch"
fi
fi
done

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.