Reputation: 15489
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?
Upvotes: 1139
Views: 831152
Reputation: 60383
To list local-only branches, local branches not tracking a remote upstream,
git for-each-ref --format='%(refname:short) %(upstream)' refs/heads \
| grep -v ' refs/remotes/'
Upvotes: 0
Reputation: 2800
Using git branch
's --format
, you can show local branches and their configured remote
and merge
configuration (as found in the repo's .git/config
file).
git branch --format='%(HEAD) %(refname:short) -> %(upstream:short)'
See the format doc for git for-each-ref
which is used by git branch
You can add it as an alias in your ~/.gitconfig
[alias]
[...]
brem = branch --format='%(HEAD) %(refname:short) -> %(upstream:short)'
[...]
origin
and upstream
upstream
is configured to push/pull to the upstream
's main
branchtest
has not been pushed anywhere and does not have a remote / merge configured> git branch --format='%(HEAD) %(refname:short) -> %(upstream:short)'
* main -> origin/main
release/v0.01 -> origin/release/v0.01
release/v0.02 -> origin/release/v0.02
test ->
upstream -> upstream/main
Upvotes: 1
Reputation: 159
Other way for get a list just local branch is:
git branch -a | grep -v 'remotes'
Upvotes: 14
Reputation: 731
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.
Upvotes: 36
Reputation: 1335
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.
Upvotes: 50
Reputation: 352
These are all the commands that you can use for branches
Show all local branches:
git branch
Show local branches with extra information:
git branch -v
It shows the last commit message along with the branch name
Show all branches (local and remote):
git branch -a
Show remote branches:
git branch -r
Show all branches with additional tracking details:
git branch -vv
This command shows both local and remote branches along with tracking information.
Show local branches sorted by their last commit date:
git branch --sort=-committerdate
This command lists local branches sorted by their last commit date, with the most recent branch first.
Show merged branches:
git branch --merged
This command lists branches that have been merged into the currently checked out branch.
Show branches that have not been merged:
git branch --no-merged
This command lists branches that have not been merged into the currently checked out branch.
Upvotes: 3
Reputation: 11
List all local branches that might have remote
git branch
To list local branches with changes that not merged to remote yet
git branch --list --no-merged
To list local that never was linked to remote
git branch -vv | awk '/: gone]/{print $1}'
Upvotes: 1
Reputation: 1535
git branch -a
- All branches.
git branch -r
- Remote branches only.
git branch
- Local branches (contains local branches linked to a remote branch).
Upvotes: 131
Reputation: 25935
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.
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:
Upvotes: 5
Reputation: 539
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 "<=" } }
}
branch both remoteOnly localOnly
------ ---- ---------- ---------
master True False False
HEAD->master False True False
renamed False True False
Upvotes: 0
Reputation: 3041
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.
Upvotes: 0
Reputation: 143
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.
Upvotes: 8
Reputation: 24864
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.
Upvotes: 1658
Reputation: 4209
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
Upvotes: 19