Shell script to check if specified Git branch exists?
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.
hzxu
Updated on September 29, 2020Comments
-
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 Igrep name
then branch with a namebranch-name
would be matched.So is there a better way to do this?
Thanks!
-
designermonkey about 10 yearsIf 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 over 9 yearsif [ "
git branch --list ${BRANCHNAME}
" ] -
Miserable Variable about 9 years@JeffCharter won't this always evaluate to true?
-
JeffCharter about 9 yearsit 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 over 7 yearsPlease don't do this until you read the other linked answer. Summary:
git show-ref
-
SuperUberDuper over 7 yearsthis is always true for me: if [ "git branch --list gh-pages" ] , how to fix?
-
Pwnrar about 7 yearsIt'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 almost 5 yearsThe 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 over 4 yearsIf 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 leadingorigin/
- that isgrep --extended-regexp "^[[:space:]]+origin/${branchname}$"
-
TTT over 4 yearsI 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 over 2 yearsI 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