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

Re: Notation for current branch?

From
Jakub Narębski <jnareb@gmail.com>
Date
Aug 28, 2016, 13:56 UTC
Message-ID
<4114ccbc-6b23-b7fe-ce4a-fcd0c9e4038a@gmail.com>
In-Reply-To
<20160828105159.GA5477@ikke.info>
W dniu 28.08.2016 o 12:51, Kevin Daudt pisze:
> On Sun, Aug 28, 2016 at 05:58:18PM +0800, ryenus wrote:
>> I wonder if there's an easy to use notation to refer to the current branch?
>> which is expected be friendly to scripting.
>>
>> For HEAD, there's @, which is short and concise.
What's wrong with simply using 'HEAD' for scripting?
Show 8 quoted lines
>>
>> But for the current branch, it seems one has to either use a not so friendly
>> plumbing command, or grep/parse the output of `git branch`, since the latter
>> doesn't even has any option to only print the plain name of the current branch,
>> or maybe an option can be added to `git branch`?
> 
> Scripts should always rely on plubming commands, never on porcelain, as
> their output will change, and thus, break scripts.
It is not something theoretical; the output of "git branch" for detached HEAD
(aka anonymous / unnamed branch) did change.
 
Show 8 quoted lines
> To get the current branch name, the best is to use `git rev-parse
> --symbolic-full-name`[1], which either returns you the current branch name
> (eg refs/heads/master), or HEAD, when you have a detached HEAD. If you
> only want the friendly name, add --abbrev-ref, which would then return
> master.
> 
> [1]: git symbolic-ref HEAD would also work, but errors out when you're
> not on a branch.

Note that in some cases current branch is implied, like e.g. in "@{<n>}" notation.

-- 
Jakub Narębski
Previous: Kevin DaudtNext: Jacob Keller
Message 3 of 9 in “Notation for current branch?”
  1. ryenusAug 28, 2016
  2. Kevin DaudtAug 28, 2016
  3. Jakub NarębskiAug 28, 2016
  4. Jacob KellerAug 29, 2016
  5. Junio C HamanoAug 29, 2016
  6. Jacob KellerAug 29, 2016
  7. ryenusSep 1, 2016
  8. ryenusAug 30, 2016
  9. Junio C HamanoAug 30, 2016

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.