Git: How do I list only local branches?

GitGit Branch

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

Just the plain command

git branch

Solution 3 - Git

git branch -a - All branches.

git branch -r - Remote branches only.

git branch - Local branches only.

Solution 4 - Git

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.

Solution 5 - Git

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.

Solution 6 - Git

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

Solution 7 - Git

Other way for get a list just local branch is:

git branch -a | grep -v 'remotes'

Solution 8 - Git

There's a great answer to a post about how to delete local-only branches. In it, the following 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.

Solution 9 - Git

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

In my Git Bash shell if I run below command:

git branch

This command (without 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 -a(stands for all) parameter:

git branch -a

Then remote branches which aren't checked out yet are also shown in different (red) color:

Enter image description here

Solution 10 - Git

Use:

git show-ref --heads

The answer by gertvdijk is the most concise and elegant, but this may help 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.

Solution 11 - Git

PowerShell users can use its Compare-Object cmdlet to do something like this:

function match-branch {
    $localBranches = ((git branch -l) -replace "\*", "") -replace " ", ""
    $remoteBranches = (((git branch -r) -replace "\*", "") -replace " ", "") -replace "origin/", ""
    Compare-Object -ReferenceObject $localBranches -DifferenceObject $remoteBranches -IncludeEqual
    | Select-Object @{Label = "branch"; Expression = { $_.InputObject } },
    @{Label = ”both”; Expression = { $_.SideIndicator -eq "==" } },
    @{Label = ”remoteOnly”; Expression = { $_.SideIndicator -eq "=>" } },
    @{Label = ”localOnly”; Expression = { $_.SideIndicator -eq "<=" } }
}
Example Output
branch        both remoteOnly localOnly
------        ---- ---------- ---------
master        True      False     False
HEAD->master False       True     False
renamed      False       True     False

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmunyengmView Question on Stackoverflow
Solution 1 - GitgertvdijkView Answer on Stackoverflow
Solution 2 - Gitc00kiemon5terView Answer on Stackoverflow
Solution 3 - GitshortduckView Answer on Stackoverflow
Solution 4 - GitVictor YaremaView Answer on Stackoverflow
Solution 5 - GitJohn MarterView Answer on Stackoverflow
Solution 6 - GitShnatselView Answer on Stackoverflow
Solution 7 - GitjlsanchezrView Answer on Stackoverflow
Solution 8 - GitSamwarView Answer on Stackoverflow
Solution 9 - GitRBTView Answer on Stackoverflow
Solution 10 - GitsdcView Answer on Stackoverflow
Solution 11 - Gitpavol.kutajView Answer on Stackoverflow