]> git.phdru.name Git - git-scripts.git/blob - detached-head/fix-detached-head
7be050aad8cf177ab93848786c112f2571220282
[git-scripts.git] / detached-head / fix-detached-head
1 #! /usr/bin/env bash
2
3 git symbolic-ref -q HEAD >/dev/null && exit
4
5 pwd
6
7 declare -a branches
8 branches=(`git branch --points-at=HEAD | tail -n +2`)
9 if [ "${#branches[*]}" -eq 0 ]; then
10    branches=(`git branch --points-at=HEAD -r | sed 's!^ *origin/!!'`)
11 fi
12
13 if [ "${#branches[*]}" -eq 0 ]; then
14    echo "Cannot find a branch" >&2
15 elif [ "${#branches[*]}" -eq 1 ]; then
16    git checkout "${branches[0]}"
17 else
18    echo "Too many branches" >&2
19 fi