44

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.

3
  • 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. Commented Aug 5, 2018 at 18:09
  • 5
    @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. Commented Aug 5, 2018 at 18:51
  • That's way: stackoverflow.com/a/72170904/990047 Commented May 9, 2022 at 11:03

1 Answer 1

50

You can use for-each-ref:

git for-each-ref --format='%(refname:short)' refs/heads/ 
Sign up to request clarification or add additional context in comments.

7 Comments

you can pass :short or lstrip=-1 to refname to get only the branch name. Like git branch --format='%(refname:short)'
@JohnnyWiller's suggestion of git branch --format also works with branch names that contain slashes, which the answer doesn't.
A quick and dirty solution git branch | cut -c 3-, who doesn't like a quick and dirty solution?
Use refs/heads/** to include branch names with slashes.
@JoeyAdams Just omitting any *s works for me too: refs/heads
|