Just curious if the following two always output the same thing (branch name)? I'm using zsh.
git rev-parse --abbrev-ref HEAD 2> /dev/null
versus
ref=$($git symbolic-ref HEAD 2> /dev/null)
echo "${ref#refs/heads/}"
If not, is one way preferred over the other for setting up git context in command prompt?
They behave the same when on a branch, but not when in "detached HEAD" mode (try git checkout --detach master
for instance, followed by git checkout master
to re-attach your head).
The real question to answer is: what do you want displayed in detached HEAD mode? If you want an abbreviated hash, use the rev-parse
format. If you want something else, use the symbolic-ref
format, perhaps with an additional clause to show "detached" or whatever, if symbolic-ref
errors out.