41

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!

hzxu
  • 5,753
  • 11
  • 60
  • 95
  • 1
    Already answered? http://stackoverflow.com/questions/5167957/is-there-a-better-way-to-find-out-if-a-local-git-branch-exists – grebneke Jan 16 '14 at 00:38
  • You caould force grep to match the whole line: git branch | grep -E "^\$branch_name$" ...or something.. – stellarhopper Jan 16 '14 at 00:47

2 Answers2

66

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
boatcoder
  • 17,525
  • 18
  • 114
  • 178
Heath
  • 2,986
  • 18
  • 21
  • 3
    if [ "`git branch --list ${BRANCHNAME}`" ] – JeffCharter Aug 23 '14 at 17:31
  • 3
    @JeffCharter won't this always evaluate to true? – Miserable Variable Apr 01 '15 at 14:10
  • 4
    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. – JeffCharter Apr 01 '15 at 22:40
  • 2
    Please don't do this until you read the [other linked answer](http://stackoverflow.com/questions/5167957/is-there-a-better-way-to-find-out-if-a-local-git-branch-exists). Summary: `git show-ref` – Todd Owen Sep 26 '16 at 04:05
  • this is always true for me: **if [ "git branch --list gh-pages" ]** , how to fix? – SuperUberDuper Nov 22 '16 at 18:07
  • 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](http://stackoverflow.com/a/9581117/2017331) – Pwnrar Mar 05 '17 at 22:35
  • 3
    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. – JamJar00 Aug 13 '19 at 10:06
  • 1
    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. – TTT Nov 06 '19 at 23:41
8

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.

David
  • 6,462
  • 2
  • 25
  • 22
  • 5
    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}$"` – designermonkey Apr 30 '14 at 14:33
  • 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}$"` – Martyn Davis Oct 29 '19 at 03:03
  • 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 – pmg7670 Jan 12 '22 at 05:36