I am using the following command to find out if a local git branch with branch-name
exists in my repository. Is this correct? Is there a better way?
Please note that I am doing this inside a script. For this reason I'd like to use plumbing commands if possible.
git show-ref --verify --quiet refs/heads/<branch-name>
# $? == 0 means local branch with <branch-name> exists.
When I search for 'git check if branch exists' on a search engine, this page is the first one I see.
I get what I want, but I'd like to provide a updated answer since the original post was from 2011.
git rev-parse --verify <branch_name>
This is essentially the same as the accepted answer, but you don't need type in "refs/heads/"