Using earlier ideas; assuming sha1 is 40 characters; and chasing references (yea, should delete the debug print lines :-):
git reflog | awk '$3 == "checkout:"&& (sha == "" || sha == $1 ) { from=$(NF - 2) to=$NF print from, to, length(from) > "/dev/stderr" if (length(from) != 40) { print from ; exit; } sha=substr(from, 1, 7) print sha > "/dev/stderr"}'
gives the raw output:
$ git statusHEAD detached at 147049c[...]$ ./gime-branch.sha47be8d2167641088b66bf8f5c2bf7d3da0c642c HEAD^ 40a47be8dmaster HEAD^ 6master