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?
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.
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.
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.
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:
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.
Powershell Users can use its Compare-Object
cmdlet to do something like this. Hope the code is self-explanatory.
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 "<=" } }
}
branch both remoteOnly localOnly
------ ---- ---------- ---------
master True False False
HEAD->master False True False
renamed False True False