Re: Git Feature Request - show current branch
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Feb 19, 2015, 18:10 UTC
- Message-ID
- <xmqqr3tlzsao.fsf@gitster.dls.corp.google.com>
- In-Reply-To
- <54E60D71.6050906@drmicha.warpmail.net>
Michael J Gruber <git@drmicha.warpmail.net> writes:
> Randall S. Becker venit, vidit, dixit 19.02.2015 14:32: >> git symbolic-ref --short HEAD > > That errors out when HEAD is detached.
Isn't that what you would want to happen anyway?
if current=$(that command)
then
you know $current is checked out
else
you know HEAD is detached
fiIf you used another command that gives either the name of the current branch or 4-letter H-E-A-D without any other indication, you cannot tell if you checked out the "HEAD" branch aka refs/heads/HEAD or you are not on any branch. The former would happen after doing this:
$ git update-ref refs/heads/HEAD HEAD
$ git checkout HEADOf course, this is not a recommended practice, and "git branch" these days refuses to create refs/heads/HEAD to discourage you from doing so by mistake, but there is no guarantee that the repository whatever script you are writing to work in was created and used by sane people ;-) so you would want to be defensive, no?