995

git branch -a shows both remote and local branches.

git branch -r shows remote branches.

Is there a way to list just the local branches?

Coleman
  • 565
  • 7
  • 15
munyengm
  • 13,831
  • 4
  • 21
  • 33

10 Answers10

1523

Just git branch without options.

From the manpage:

With no arguments, existing branches are listed and the current branch will be highlighted with an asterisk.

LHM
  • 557
  • 8
  • 25
gertvdijk
  • 21,668
  • 5
  • 32
  • 56
  • 34
    I was hoping to find a way to list local branches that have no corresponding remote branch. – Steve Crane Jun 10 '14 at 14:57
  • 5
    Not completely but answers to my question [How do I list local branches that have no remote branch](http://stackoverflow.com/questions/24144602/git-how-do-i-list-local-branches-that-have-no-remote-branch) provide some help. – Steve Crane Jun 17 '14 at 11:10
  • 3
    @c00kiemon5ter I love how you found a way to get a bunch of points anyway! So funny. – Abram Jun 01 '15 at 15:21
  • @SteveCrane here's how you list local branches without a remote branch with the same name in origin: http://stackoverflow.com/a/41638933/585725 – Shnatsel Jan 13 '17 at 16:14
  • 4
    how is this the right answer to what was asked ("... to list ***just the local** branches...")??? – Wagner Danda da Silva Filho Nov 09 '17 at 19:43
  • 1
    @wdanda It does list only local branches. If you are looking for an answer to something like "How do I list branches that do not exist in any remote?" that's something totally different with Git. – gertvdijk Nov 13 '17 at 20:19
  • @gertvdijk @wdanda - but that what my english skills tells me it's what he asking `Git: How do I list only local branches?` = that is list *only* the local branches, why would he put *only* if he wanted "How do I list local branches". Semantics - the `only local branch` is imho the branch which is *only local* - not *local and remote* – gr4viton Nov 22 '17 at 11:47
  • @gr4viton But it lists only local branches. If you set up a local branch to track a remote branch, it's still a local branch. What other definition of a local branch do you have? Do you want to exclude branches created locally that also exist on any of the remotes? And what if they are named differently on a remote? It's still a very vague definition of local imo. It can be different for anyone as well. – gertvdijk Nov 26 '17 at 18:21
  • 9
    @gr4viton: In the dialects of English that I'm familiar with, "list only local branches" usually parses as "list only those branches that are local". (To say "list those branches that are only local", I would say "list local-only branches".) – Mathieu K. Mar 16 '18 at 02:46
231

just the plain command

git branch
c00kiemon5ter
  • 14,786
  • 5
  • 43
  • 44
87

git branch -a - All branches.

git branch -r - Remote branches only.

git branch -l or git branch - Local branches only.

avivamg
  • 5,638
  • 1
  • 37
  • 32
shortduck
  • 1,095
  • 6
  • 10
  • 7
    To me this answer’s formatting suggests the `-l` stands for `--local`, while it’s actually `--list`. I would suggest removing it to avoid that confusion – technically all of the commands in this answer could use `-l` and they would still return the same results. – Thibaud Colas May 11 '20 at 10:35
37

One of the most straightforward ways to do it is

git for-each-ref --format='%(refname:short)' refs/heads/

This works perfectly for scripts as well.

Victor Yarema
  • 859
  • 9
  • 14
35

If the leading asterisk is a problem, I pipe the git branch as follows

git branch | awk -F ' +' '! /\(no branch\)/ {print $2}'

This also eliminates the '(no branch)' line that shows up when you have detached head.

John Marter
  • 641
  • 7
  • 4
14

Here's how to list local branches that do not have a remote branch in origin with the same name:

git branch | sed 's|* |  |' | sort > local
git branch -r | sed 's|origin/||' | sort > remote
comm -23 local remote
wjandrea
  • 16,334
  • 5
  • 30
  • 53
Shnatsel
  • 3,453
  • 1
  • 21
  • 22
11

Other way for get a list just local branch is:

git branch -a | grep -v 'remotes'
jlsanchezr
  • 121
  • 1
  • 4
6

There's a great answer to a post about how to delete local only branches. In it, the fellow builds a command to list out the local branches:

git branch -vv | cut -c 3- | awk '$3 !~/\[/ { print $1 }'

The answer has a great explanation about how this command was derived, so I would suggest you go and read that post.

Samwar
  • 111
  • 1
  • 4
  • 1
    Thank you for linking the answer. I needed an algo to list local branches that **DO NOT** track a remote. This one is the only one that does the job. – JuroOravec Apr 26 '20 at 18:53
3

To complement @gertvdijk's answer - I'm adding few screenshots in case it helps someone quick.

On my git bash shell

git branch

command without any parameters shows all my local branches. The current branch which is currently checked out is shown in different color (green) along with an asterisk (*) prefix which is really intuitive.

enter image description here

When you try to see all branches including the remote branches using

git branch -a

command then remote branches which aren't checked out yet are shown in red color:

enter image description here

RBT
  • 18,275
  • 13
  • 127
  • 181
  • 1
    A thousand words from which one can't copy? – Tony Adams Mar 02 '18 at 22:21
  • 3
    @TonyAdams I'm sorry if I'm missing something but both the commands whose output I've displayed in the pictures were mentioned in grey background (code formatting) in the post. Never mind I've improved the formatting of the post to make it even more readable. Appreciate your feedback. – RBT Mar 02 '18 at 23:50
0
git show-ref --heads

The answer by @gertvdijk is the most concise and elegant, but I wanted to leave this here because it helped me grasp the idea that refs/heads/* are equivalent to local branches.

Most of the time the refs/heads/master ref is a file at .git/refs/heads/master that contains a git commit hash that points to the git object that represents the current state of your local master branch, so each file under .git/refs/heads/* represents a local branch.

sdc
  • 1,830
  • 16
  • 33