Manoj Govindan Manoj Govindan - 1 year ago 60
Git Question

Is there a better way to find out if a local git branch exists?

I am using the following command to find out if a local git branch with

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 stay away from porcelain commands if possible.

git show-ref --verify --quiet refs/heads/<branch-name>
# $? == 0 means local branch with <branch-name> exists.


Turns out there is another way. Thanks @jhuynh.

git rev-parse --verify <branch_name>
# $? == 0 means local branch with <branch-name> exists.

Answer Source

As far as I know, that's the best way to do it in a script. I'm not sure there's much more to add to add to that, but there might as well be one answer that just says "That command does everything you want" :)

The only thing you might want to be careful of is that branch names can have surprising characters in them, so you may want to quote <branch-name>.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download