git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Git Feature Request - show current branch

From
Michael J Gruber <git@drmicha.warpmail.net>
Date
Feb 20, 2015, 10:16 UTC
Message-ID
<54E70987.4060904@drmicha.warpmail.net>
In-Reply-To
<xmqqr3tlzsao.fsf@gitster.dls.corp.google.com>
Junio C Hamano venit, vidit, dixit 19.02.2015 19:10:
Show 31 quoted lines
> 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
> 	fi
> 
> If 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 HEAD
> 
> Of 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?
> 

If you want to be defenive, don't use --abbrev-ref, just git rev-parse --symbolic-full-name HEAD

The original request was for a one-liner, I think.
Michael
Previous: Junio C Hamano
Message 5 of 5 in “Git Feature Request - show current branch”
  1. mdconf@seznam.czFeb 19, 2015
  2. Randall S. BeckerFeb 19, 2015
  3. Michael J GruberFeb 19, 2015
  4. Junio C HamanoFeb 19, 2015
  5. Michael J GruberFeb 20, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.