threads / discuss / 38601

Git Feature Request - show current branch

Subject: Git Feature Request - show current branch

## tl;dr

5 messages between Feb 19, 2015 and Feb 20, 2015.

replies: 4people: 4as markdown or json

mdconf@seznam.cz· Feb 19, 2015, 13:14 UTC · lore
Hello,
To start with, I did not find an official way to submit feature request so hopefully this is the right way to do so - if not then my apologize & appreciate if somebody could re-submit to the proper place.
I'd like to request adding a parameter to 'git branch' that would only show the current branch (w/o the star) - i.e. the outcome should only be the name of the branch that is normally marked with the star when I do 'git branch' command. This may be very helpful in some external scripts that just simply need to know the name of the current branch. I know there are multiple ways to do this today (some described here: http://stackoverflow.com/questions/6245570/how-to-get-current-branch-name-in-git) but I really think that adding simple argument to 'git branch' would be very useful instead of forcing people to use 'workarounds'.

My suggestion is is to name the parameter '--current' or '--show-current'. Example:

Command: git branch
Outcome:
 branchA
 branchB
* master
Command: git branch --current
Outcome:
master

Thank you, Martin

Randall S. Becker· Feb 19, 2015, 13:32 UTC · re: mdconf@seznam.cz · lore

RE: Git Feature Request - show current branch

Hi Martin,
I use:
git symbolic-ref --short HEAD
in scripts. Not sure it's the best way, but it works 100% for me.

Regards, Randall

-----Original Message-----
From: git-owner@vger.kernel.org [mailto:git-owner@vger.kernel.org] On Behalf Of mdconf@seznam.cz
Sent: February 19, 2015 8:15 AM
To: git@vger.kernel.org
Subject: Git Feature Request - show current branch
Hello,
To start with, I did not find an official way to submit feature request so hopefully this is the right way to do so - if not then my apologize & appreciate if somebody could re-submit to the proper place.
I'd like to request adding a parameter to 'git branch' that would only show the current branch (w/o the star) - i.e. the outcome should only be the name of the branch that is normally marked with the star when I do 'git branch' command. This may be very helpful in some external scripts that just simply need to know the name of the current branch. I know there are multiple ways to do this today (some described here: http://stackoverflow.com/questions/6245570/how-to-get-current-branch-name-in-git) but I really think that adding simple argument to 'git branch' would be very useful instead of forcing people to use 'workarounds'.

My suggestion is is to name the parameter '--current' or '--show-current'. Example:

Command: git branch
Outcome:
 branchA
 branchB
* master
Command: git branch --current
Outcome:
master

Thank you, Martin -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html

Michael J Gruber· Feb 19, 2015, 16:21 UTC · re: Randall S. Becker · lore

Re: Git Feature Request - show current branch

Randall S. Becker venit, vidit, dixit 19.02.2015 14:32:
> git symbolic-ref --short HEAD
That errors out when HEAD is detached.
git rev-parse --symbolic-full-name [--abbrev-ref] HEAD
returns the branch name or HEAD. Though it's a bit difficult to discover.
I guess git 3.0 will have "git branch" and "git branches" :)
Michael
Junio C Hamano· Feb 19, 2015, 18:10 UTC · re: Michael J Gruber · lore

Re: Git Feature Request - show current branch

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?

Michael J Gruber· Feb 20, 2015, 10:16 UTC · re: Junio C Hamano · lore

Re: Git Feature Request - show current branch

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

← back to recent threads