Git: How do I list only local branches?

a 夏天 提交于 2019-11-28 02:31:49

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.

just the plain command

git branch
shortduck

git branch -a - all branches.

git branch -r - remote branches only.

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

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.

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.

Shnatsel

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

Other way for get a list just local branch is:

git branch -a | grep -v 'remotes'

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.

Just to complement @gertvdijk's answer - a picture says a thousand words 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 also shown in different color (green) along with an asterisk 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:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!