# Notation for current branch?

9 messages from 2016-08-28 to 2016-09-01. Participants: ryenus, Kevin Daudt, Jakub Narębski, Jacob Keller, Junio C Hamano.
Thread: https://gitlist.dev/t/43947

## ryenus, 2016-08-28 09:58

Subject: Notation for current branch?
Message-ID: <CAKkAvazQxhtQauSb3MTVhzoK=xWbjMzQLBPQZUpp1n7VMUMJbQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAKkAvazQxhtQauSb3MTVhzoK%3DxWbjMzQLBPQZUpp1n7VMUMJbQ%40mail.gmail.com

```
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, 2016-08-28 10:51

Subject: Re: Notation for current branch?
Message-ID: <20160828105159.GA5477@ikke.info>
URL: https://gitlist.dev/e/20160828105159.GA5477%40ikke.info
In-Reply-To: <CAKkAvazQxhtQauSb3MTVhzoK=xWbjMzQLBPQZUpp1n7VMUMJbQ@mail.gmail.com>

```
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.
> 
> 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, 2016-08-28 13:56

Subject: Re: Notation for current branch?
Message-ID: <4114ccbc-6b23-b7fe-ce4a-fcd0c9e4038a@gmail.com>
URL: https://gitlist.dev/e/4114ccbc-6b23-b7fe-ce4a-fcd0c9e4038a%40gmail.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?

>>
>> 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.
 
> 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, 2016-08-29 06:39

Subject: Re: Notation for current branch?
Message-ID: <CA+P7+xrGh687cYHJmYyXiWfjg_8i2-31FZOvk4bEMsO1FU22WA@mail.gmail.com>
URL: https://gitlist.dev/e/CA%2BP7%2BxrGh687cYHJmYyXiWfjg_8i2-31FZOvk4bEMsO1FU22WA%40mail.gmail.com
In-Reply-To: <4114ccbc-6b23-b7fe-ce4a-fcd0c9e4038a@gmail.com>

```
On Sun, Aug 28, 2016 at 6:56 AM, Jakub Narębski <jnareb@gmail.com> wrote:
> 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, 2016-08-29 19:49

Subject: Re: Notation for current branch?
Message-ID: <xmqqy43f9wzh.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqy43f9wzh.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <CA+P7+xrGh687cYHJmYyXiWfjg_8i2-31FZOvk4bEMsO1FU22WA@mail.gmail.com>

```
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, 2016-08-29 19:52

Subject: Re: Notation for current branch?
Message-ID: <CA+P7+xpz8UNt6bBgrJvsHdFE+MQ4J80K1ed+b41XH_CG6HYYTA@mail.gmail.com>
URL: https://gitlist.dev/e/CA%2BP7%2Bxpz8UNt6bBgrJvsHdFE%2BMQ4J80K1ed%2Bb41XH_CG6HYYTA%40mail.gmail.com
In-Reply-To: <xmqqy43f9wzh.fsf@gitster.mtv.corp.google.com>

```
On Mon, Aug 29, 2016 at 12:49 PM, Junio C Hamano <gitster@pobox.com> wrote:
> 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, 2016-08-30 18:09

Subject: Re: Notation for current branch?
Message-ID: <CAKkAvaxyEMjASzfGTqt73AtW7ag-YfqN1yVLEhFreiU-UVdnkA@mail.gmail.com>
URL: https://gitlist.dev/e/CAKkAvaxyEMjASzfGTqt73AtW7ag-YfqN1yVLEhFreiU-UVdnkA%40mail.gmail.com
In-Reply-To: <xmqqy43f9wzh.fsf@gitster.mtv.corp.google.com>

```
On 30 August 2016 at 03:49, Junio C Hamano <gitster@pobox.com> wrote:
> 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, 2016-08-30 18:19

Subject: Re: Notation for current branch?
Message-ID: <xmqqoa4a3yrl.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqoa4a3yrl.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <CAKkAvaxyEMjASzfGTqt73AtW7ag-YfqN1yVLEhFreiU-UVdnkA@mail.gmail.com>

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


```

## ryenus, 2016-09-01 15:51

Subject: Re: Notation for current branch?
Message-ID: <CAKkAvaxD75sC-S7QBG6gXYbSMmBz5hpe8ZEYRRUt_BE99yvrYg@mail.gmail.com>
URL: https://gitlist.dev/e/CAKkAvaxD75sC-S7QBG6gXYbSMmBz5hpe8ZEYRRUt_BE99yvrYg%40mail.gmail.com
In-Reply-To: <CA+P7+xpz8UNt6bBgrJvsHdFE+MQ4J80K1ed+b41XH_CG6HYYTA@mail.gmail.com>

```
> 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 :(

```
