20

If I run git branch, I get something like:

* master
  dev
  foo

if I do git branch -r, it will show all branches on the remote, without an asterisk (where the asterisk shows my current checked-out branch).

How can I list all the local branches, without an asterisk showing up? I need a programmatic solution so I can read in the results line-by-line.

  • You can just trim the leading whitespaces and remove the asterisk by using regular expressions. I'm sure the programming language you are using makes this possible. – tangoal Aug 05 '18 at 18:09
  • 3
    @tangoal: you *can* do this, but that's almost certainly not the best way. The Git authors divide their commands loosely into what they call *plumbing* and *porcelain*. A plumbing command is meant for other commands to use, while a porcelain command is meant for humans to use. Hence the output of a plumbing command is typically best-suited as input to another program, rather than for eyeballing. `git for-each-ref` is the plumbing command that implements `git branch --list`, `git tag --list`, and several others. – torek Aug 05 '18 at 18:51

1 Answers1

23

You can use for-each-ref:

git for-each-ref --format='%(refname:short)' refs/heads/
knittl
  • 197,664
  • 43
  • 269
  • 318