git branch command is incompatible with bash

2 messages, 2 authors, 2016-06-15 · open the first message on its own page

git branch command is incompatible with bash

From: Anatol Rudolph <hidden>
Date: 2016-06-15 23:05:53

Hello!

I hope posting this to this mailing list is okay, this is the first ever
mail that I submit to a technical mailing list. 

When using the git branch command, git uses a '*' to denote the current
branch. Therefore, in bash this:

	$ branchName=$(git branch -q)
	$ echo $branchName

produces a directory listing, because the '*' is interpreded by the
shell. 

While an (unwieldly) workaround exists:

	$ branchName=$(git symbolic-ref -q HEAD)                                                                            
	$ branchName=${branch##refs/heads/}

it would still be nice, if there were a --current flag, that returned
only the current branch name, omitting the star:

	$ branchName=$(git branch --current -q)
	$ echo $branchName
	master

Re: git branch command is incompatible with bash

From: Johannes Sixt <hidden>
Date: 2016-06-15 23:05:54

Am 27.07.2015 um 14:12 schrieb Anatol Rudolph:
When using the git branch command, git uses a '*' to denote the current
branch. Therefore, in bash this:

	$ branchName=$(git branch -q)
	$ echo $branchName

produces a directory listing, because the '*' is interpreded by the
shell.
Of course. You would write the last line as

   echo "$branchName"

These are shell fundamentals.
While an (unwieldly) workaround exists:

	$ branchName=$(git symbolic-ref -q HEAD)
	$ branchName=${branch##refs/heads/}
If you want to do that in a script, this is not a work-around, but it is 
how you should do it. But you may want to use option --short to save the 
second line.
it would still be nice, if there were a --current flag, that returned
only the current branch name, omitting the star:

	$ branchName=$(git branch --current -q)
	$ echo $branchName
	master
Try

   branchName=$(git rev-parse --abbrev-ref HEAD)

-- Hannes
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help