threads / discuss / 43947

Notation for current branch?

Subject: Notation for current branch?

## tl;dr

9 messages between Aug 28, 2016 and Sep 1, 2016.

replies: 8people: 5as markdown or json

ryenus· Aug 28, 2016, 09:58 UTC · lore

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.

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`?

Thoughts?
Kevin Daudt· Aug 28, 2016, 10:51 UTC · re: ryenus · lore

Re: Notation for current branch?

On Sun, Aug 28, 2016 at 05:58:18PM +0800, ryenus wrote:
Show 11 quoted lines
> 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.
> 
> 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`?
> 
> Thoughts?

Scripts should always rely on plubming commands, never on porcelain, as their output will change, and thus, break scripts.

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.

Jakub Narębski· Aug 28, 2016, 13:56 UTC · re: Kevin Daudt · lore

Re: Notation for current branch?

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
Jacob Keller· Aug 29, 2016, 06:39 UTC · re: Jakub Narębski · lore

Re: Notation for current branch?

On Sun, Aug 28, 2016 at 6:56 AM, Jakub Narębski <jnareb@gmail.com> wrote:
Show 10 quoted lines
> 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?
>

When you want to display the current branch to the user, e.g. when scripting a shell prompt or similar use

Thanks, Jake

Junio C Hamano· Aug 29, 2016, 19:49 UTC · re: Jacob Keller · lore

Re: Notation for current branch?

Jacob Keller <jacob.keller@gmail.com> writes:
>> What's wrong with simply using 'HEAD' for scripting?
>
> When you want to display the current branch to the user, e.g. when
> scripting a shell prompt or similar use

Wait. Even if a hypothetical version of Git understood @@ as "the current branch", how would you use that notation, instead of HEAD, to decide what to "display the current branch to the user"?

You obviously will not be doing
	echo @@

You would be doing something around "git symbolic-ref" at the very least, and wouldn't you be feeding HEAD to that symbolic-ref anyway while doing so?

Jacob Keller· Aug 29, 2016, 19:52 UTC · re: Junio C Hamano · lore

Re: Notation for current branch?

On Mon, Aug 29, 2016 at 12:49 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 19 quoted lines
> Jacob Keller <jacob.keller@gmail.com> writes:
>
>>> What's wrong with simply using 'HEAD' for scripting?
>>
>> When you want to display the current branch to the user, e.g. when
>> scripting a shell prompt or similar use
>
> Wait.  Even if a hypothetical version of Git understood @@ as "the
> current branch", how would you use that notation, instead of HEAD,
> to decide what to "display the current branch to the user"?
>
> You obviously will not be doing
>
>         echo @@
>
> You would be doing something around "git symbolic-ref" at the very
> least, and wouldn't you be feeding HEAD to that symbolic-ref anyway
> while doing so?
>

Hmm. You are right, I mis-understood the original question. Use of "git symbolic-ref" seems like the right thing if you need to obtain the current branch name, and there's no reason to not just use HEAD there.

Thanks, Jake

ryenus· Sep 1, 2016, 15:51 UTC · re: Jacob Keller · lore

Re: Notation for current branch?

> Jacob Keller <jacob.keller@gmail.com> writes:
> "git symbolic-ref" seems like the right thing if you need to obtain
> the current branch name, and there's no reason to not just use HEAD
> there.

Really? Any reason why `git rev-parse --abbrev-ref '@{-1}'` works, but not `git symbolic-ref '@{-1}'`, or even `git symbolic-ref @`?

BTW, possible to make the curly brances optional in `@{n}`, `@{-n}`, and the alike? coz they require quoting/escaping, and more typing :(

ryenus· Aug 30, 2016, 18:09 UTC · re: Junio C Hamano · lore

Re: Notation for current branch?

On 30 August 2016 at 03:49, Junio C Hamano <gitster@pobox.com> wrote:
Show 19 quoted lines
> Jacob Keller <jacob.keller@gmail.com> writes:
>
>>> What's wrong with simply using 'HEAD' for scripting?
>>
>> When you want to display the current branch to the user, e.g. when
>> scripting a shell prompt or similar use
>
> Wait.  Even if a hypothetical version of Git understood @@ as "the
> current branch", how would you use that notation, instead of HEAD,
> to decide what to "display the current branch to the user"?
>
> You obviously will not be doing
>
>         echo @@
>
> You would be doing something around "git symbolic-ref" at the very
> least, and wouldn't you be feeding HEAD to that symbolic-ref anyway
> while doing so?
>

For now the best use case I can think of is with git-reflog, e.g., the meaning of `git reflog HEAD` and `git reflog feature-branch` are quite different, even if I'm currently on the feature-branch, especially when I want to track the rebase histories (if any).

If there's a notation for the current branch then I don't have to find the name of the current branch, then copy/paste it. However, in this case, maybe git-reflog can have a `-b` option to show the reflog of the current branch (if on any)?

- ryenus
Junio C Hamano· Aug 30, 2016, 18:19 UTC · re: ryenus · lore

Re: Notation for current branch?

ryenus <ryenus@gmail.com> writes:
> For now the best use case I can think of is with git-reflog, e.g.,
> the meaning of `git reflog HEAD` and `git reflog feature-branch`
> are quite different, even if I'm currently on the feature-branch,
> especially when I want to track the rebase histories (if any).

"git reflog" gives you the reflog of HEAD in numbered format (HEAD@{1}, HEAD@{2}, etc.), and "git reflog HEAD@{now}" is an interesting way to tell it "I want that same HEAD reflog but not in numbered but in timed format).

"git reflog @{0}" and "git reflog @{now}" give you the reflog of the current branch in these formats.

← back to recent threads