Shell script to check if specified Git branch exists?

34,385

Solution 1

NOTE: This always returns true. This is not the right answer to the question, even though it has been accepted....

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` ]
then
   echo "Branch name $branch_name already exists."
fi

Solution 2

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}$"` ]
then
    echo "Branch exists"
fi

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

Share:
34,385
hzxu
Author by

hzxu

Updated on September 29, 2020

Comments

  • hzxu
    hzxu over 3 years

    I need to create a Git branch using shell script, but since the branch may exist, I need to be aware of that. Currently I'm using:

    if [ `git branch | grep $branch_name` ]
    then
        echo "Branch named $branch_name already exists"
    else
        echo "Branch named $branch_name does not exist"
    fi
    

    But the problem is the grep command finds branch name without matching the exact name, that is, if I grep name then branch with a name branch-name would be matched.

    So is there a better way to do this?

    Thanks!

  • designermonkey
    designermonkey about 10 years
    If you're on the branch, then there is a star at the beginning. This works better for a proper match git branch | egrep "^\*?[[:space:]]+${BRANCH}$"
  • JeffCharter
    JeffCharter over 9 years
    if [ "git branch --list ${BRANCHNAME}" ]
  • Miserable Variable
    Miserable Variable about 9 years
    @JeffCharter won't this always evaluate to true?
  • JeffCharter
    JeffCharter about 9 years
    it was a auto-format problem (backtic was converted to code marker) if [ "`git branch --list master`" ]; then echo hi; fi the answer actually always returns true... it is basically the same as my mis-post.
  • Todd Owen
    Todd Owen over 7 years
    Please don't do this until you read the other linked answer. Summary: git show-ref
  • SuperUberDuper
    SuperUberDuper over 7 years
    this is always true for me: if [ "git branch --list gh-pages" ] , how to fix?
  • Pwnrar
    Pwnrar about 7 years
    It's important to remember to leave a space on either side of the square brackets. In Bash the square bracket is actually a command so those spaces are syntactically important. explanation
  • JamJar00
    JamJar00 almost 5 years
    The solution to this always returning true is probably just to check the returned content isn't empty: if [ -n "$(git branch --list $branch_name)" ]. Untested though.
  • Martyn Davis
    Martyn Davis over 4 years
    If the branch does not exist locally, then you should verify that the branch exists on the remote. Use git branch --remotes. There is no concept of current branch, so there will be no leading * in this output. But you do need to search for the branch name with a leading origin/ - that is grep --extended-regexp "^[[:space:]]+origin/${branchname}$"
  • TTT
    TTT over 4 years
    I don't understand why the note is there about this always returning true. I added an else clause and this works perfectly for me. Branches that are there hit the if, and branches that aren't hit the else.
  • pmg7670
    pmg7670 over 2 years
    I tested this with my own repo, with multiple branches. It worked fine for the non-active branches and a non-existent branch name, but failed for the active branch with : line 8: [: too many arguments. I'm totally new to bash scripting but I discovered that [ ] is the old style built-in test, and it treats "* main" stdin as 2 arguments. By using the new style test built-in [[ git branch | egrep "^\*?\s+${BRANCH}$ ]] solved this error for me