Sorry this is another command-line answer, but that's what I was looking for when I found this question and many of these answers were helpful. My solution is the following bash shell function:
get_branch () { git rev-parse --abbrev-ref HEAD | grep -v HEAD || \ git describe --exact-match HEAD 2> /dev/null || \ git rev-parse HEAD}
This should always give me something both human-readable and directly usable as an argument to git checkout
.
- on a local branch:
feature/HS-0001
- on a tagged commit (detached):
v3.29.5
- on a remote branch (detached, not tagged): SHA1
- on any other detached commit: SHA1