#! /usr/bin/env bash git symbolic-ref -q HEAD >/dev/null && exit pwd declare -a branches branches=(`git branch --points-at=HEAD | tail -n +2`) if [ "${#branches[*]}" -eq 0 ]; then branches=(`git config -f $toplevel/.gitmodules --get submodule.$name.branch`) fi if [ "${#branches[*]}" -eq 0 ]; then branches=(`git for-each-ref --format="%(refname:lstrip=3)" --points-at=HEAD refs/remotes | sort -u`) fi if [ "${#branches[*]}" -eq 0 ]; then echo "Cannot find a branch" >&2 elif [ "${#branches[*]}" -eq 1 ]; then git checkout "${branches[0]}" else echo "Too many branches" >&2 fi