X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=detached-head%2Ffix-detached-head;fp=detached-head%2Ffix-detached-head;h=7be050aad8cf177ab93848786c112f2571220282;hb=0f2ee1cb405df27ca3d71d6794e16e617187e97d;hp=0000000000000000000000000000000000000000;hpb=7bc56813eb2d43b7abe2a4c29b9b5a79d93e5077;p=git-scripts.git diff --git a/detached-head/fix-detached-head b/detached-head/fix-detached-head new file mode 100755 index 0000000..7be050a --- /dev/null +++ b/detached-head/fix-detached-head @@ -0,0 +1,19 @@ +#! /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 branch --points-at=HEAD -r | sed 's!^ *origin/!!'`) +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