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

Re: git branch command is incompatible with bash

From
Johannes Sixt <j6t@kdbg.org>
Date
Jul 27, 2015, 21:11 UTC
Message-ID
<55B69E68.90306@kdbg.org>
In-Reply-To
<20150727121253.GC17338@2vizcon.com>
Am 27.07.2015 um 14:12 schrieb Anatol Rudolph:
Show 8 quoted lines
> 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.

Show 6 quoted lines
> 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
Previous: Anatol RudolphNext: Junio C Hamano
Message 2 of 9 in “git branch command is incompatible with bash”
  1. Anatol RudolphJul 27, 2015
  2. Johannes SixtJul 27, 2015
  3. Junio C HamanoJul 27, 2015
  4. Johannes SixtJul 28, 2015
  5. Jakub NarębskiJul 28, 2015
  6. Junio C HamanoJul 28, 2015
  7. Jeff KingJul 28, 2015
  8. Johannes SixtJul 28, 2015
  9. Scott SchmitJul 29, 2015

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.