Shell script to check if specified Git branch exists?

This question already has an answer here:

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

    11 answers

  • Puppet exec command runs in shell, but not via puppet
  • Pyexcel_xlsx successfully installed but importing is still on syntax error
  • Can hard links get broken?
  • Speed up bash script that uses several find commands
  • How can I run recursive find and replace operations on multiple files in parallel?
  • TortiseSVN svn+ssh Error: Unable to connect to a repository at URL … Network connection closed unexpectedly
  • git merge says Already up-to-date but there's a committed difference in the branches
  • my local git repo has “not staged” deleted files, remote is file, how do I pull the missing files?
  • how to completely replace code in a git repository
  • How to rebase local branch with remote master
  • How can I upload my application to github but remove sensitive authorization information?
  • Change Git repository directory location.
  • 2 Solutions collect form web for “Shell script to check if specified Git branch exists?”

    You could always use word boundaries around the name like \< and \>, but instead let Git do the work for you:

    if [ `git branch --list $branch_name `]
       echo "Branch name $branch_name already exists."

    I like Heath’s solution, but if you still want to pipe to grep, you can use regex anchors, similar to the following, to preclude matching a substring:

    if [ `git branch | egrep "^[[:space:]]+${branchname}$"` ]
        echo "Branch exists"

    Note that you need to use the space character class because the output of the command is indented.

    Git Baby is a git and github fan, let's start git clone.