Use git show-ref instead of git branch for better future-proofness.

Based on original patch by Anrs Hu in #966.

Passes all tests; I think it reproduces the previous behavior but more reliably.