Reputation: 1439
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?
Upvotes: 1
Views: 707
Reputation: 490078
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.
Upvotes: 3
Reputation: 142652
@torec posted an excellent explanation of the differences between the 2, i just want to take it a bit further and to explain a bit more about the syntx of each command.
git rev-parse --abbrev-ref HEAD 2> /dev/null
rev-parse
--abbrev-ref[=(strict|loose)]
A non-ambiguous short name of the objects name.
The optioncore.warnAmbiguousRefs
is used to select the strict abbreviation mode.
HEAD
Read here all about HEAD (+detached HEAD
) here
So what this command does it simply get the SHA-1 of HEAD
(rev-parse
), and them converts it tote branch name (--abbrev-ref
)
git symbolic-ref HEAD
symbolic-ref
Given one argument, reads which branch head the given symbolic ref refers to and outputs its path, relative to the.git/
directory.Typically you would give
HEAD
as the argument to see which branch your working tree is on
The ( symbolic-ref ) is usually used if you wish to do something regrading refs (or refspec(
Upvotes: 1