266

I am using the following command to find out if a local git branch with branch-name 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 use plumbing commands if possible.

git show-ref --verify --quiet refs/heads/<branch-name>
# $? == 0 means local branch with <branch-name> exists. 
7
  • Your command looks like the one I had used. Mar 2, 2011 at 15:21
  • 8
    In my ignorance, I'd have gone with git branch | grep -w <branch-name>. Ok, it's a porcelain command, but I can't imagine this particular usage to be change significantly in the future as to make this snippet unusable...
    – UncleZeiv
    Mar 2, 2011 at 16:27
  • @UncleZeiv: You are probably right that the command wouldn't change significantly to make it unusable. I have a bit of an OCD about such things like porcelain versus plumbing, that's all. Mar 2, 2011 at 16:37
  • 12
    git rev-parse --verify <branch_name> verifies also other references such as tags and commit hashes, so although it might be more suitable for what you need it will return false positives if you are only interested precisely in branches.
    – Paul S.
    Jun 22, 2015 at 13:37
  • 2
    Only the git show-ref is working to determine if a LOCAL branch is present. In the update (syntax with git rev-parse), the return code is also 0 if a remote branch matches. Sep 5, 2017 at 11:24

18 Answers 18

152

When I search for 'git check if branch exists' on a search engine, this page is the first one I see.

I get what I want, but I'd like to provide a updated answer since the original post was from 2011.

git rev-parse --verify <branch_name>

This is essentially the same as the accepted answer, but you don't need type in "refs/heads/<branch_name>"

So from a shell script, this would be

if [ `git rev-parse --verify main 2>/dev/null` ]
then
   ...
fi
8
  • 35
    Just a note: git rev-parse --verify only tells you if such an object exists in the repo (i.e. it'll return 0 for any value of <branch_name> that translates to an object of any type in the repo). It doesn't tell you if that object is a branch or not.
    – tavnab
    Oct 5, 2015 at 18:24
  • 18
    This is not a correct answer to the question, which is asking how to know if a branch exists. This will give you a false positive for a tag. You can easily test this yourself. You need refs/heads/ to distinguish from tags which are in refs/tags, and even remotes in refs/remotes.
    – msouth
    Jun 30, 2016 at 20:08
  • 25
    git rev-parse --verify gh-pages gives me: fatal: Needed a single revision Nov 22, 2016 at 18:10
  • 5
    This gives me fatal: Needed a single revision as well.
    – HDJEMAI
    Jan 26, 2022 at 9:39
  • 2
    There's no need to use [ ... ]. Just if git rev-parse --verify main >/dev/null 2>&1 ; then .... It uses the success/failure status returned by the command. (As noted, it returns true for both branches and tags.) Feb 14 at 5:07
78

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 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>.

2
  • 1
    good point about quoting <branch-name>. FWIW I am using this in a fabric script. I'll remember to quote the variable. Mar 2, 2011 at 16:33
  • 1
    The correct answer is this implicitly: git show-ref --verify --quiet refs/heads/<branch-name> will show that HEAD is not a branch correctly. git rev-parse --verify will tell you HEAD is an existing branch. False because HEAD is not a branch. Jul 13, 2017 at 6:46
68

I recommend git show-ref --quiet refs/heads/$name.

  • --quiet means there is no output, which is good because then you can cleanly check the exit status.

  • refs/heads/$name limits to local branches and matches full names (otherwise dev would match develop)

Usage in a script:

if git show-ref --quiet refs/heads/develop; then
    echo develop branch exists
fi
3
  • 1
    Yup this is the only one that does it silently. The naming of git commands is a bit wack tbh
    – smac89
    Sep 17, 2017 at 5:56
  • 2
    Weird that i had to scroll down three answers to get the best solution. I suppose this answer will eventually make it to the top.
    – ijoseph
    Jan 31, 2021 at 0:05
  • 1
    This is the answer to use if you want to use it in a script.
    – user151841
    Aug 17, 2022 at 15:46
47

Almost there.

Just leave out the --verify and --quiet and you get either the hash if the branch exists or nothing if it doesn't.

Assign it to a variable and check for an empty string.

exists=`git show-ref refs/heads/<branch-name>`
if [ -n "$exists" ]; then
    echo 'branch exists!'
fi
2
  • 19
    The return value is sufficient--you don't need to go through the possibly error-prone effort of assigning to a variable.
    – msouth
    Jun 30, 2016 at 20:10
  • Downvoted because you can just check the return value. Apr 11, 2021 at 5:07
30

For use in a script:

git show-ref -q --heads <branch-name>

This will exit 0 if and only if <branch-name> exists as a local branch.

Example:

if git show-ref -q --heads <branch-name>; then
   echo 'Branch exists'
fi
16

I think you can use git show-branch here.

$ git show-branch --list
  [master] test
* [testbranch] test
$ git show-branch testbranch
[testbranch] test
$ echo $?
0
$ git show-branch nonexistantbranch
fatal: bad sha1 reference nonexistantbranch
$ echo $?
128

So, $? == 0 would indicate that the branch exists and you don't have to dig in to the plumbing of refs/heads/ at all. As long as you don't pass -r to show-branch, it will only operate on local branches.

3
  • 6
    AFAIK git show-branch is a porcelain command. As I've said in my question I'd rather not use porcelain commands in a script if plumbing equivalents are available. See kernel.org/pub/software/scm/git/docs Mar 2, 2011 at 13:36
  • 4
    @Manoj: I know about porcelain vs. plumbing, but I had never read that the plumbing was considered to be more stable than the porcelain. Thanks for pointing me to that in the docs.
    – Mark Drago
    Mar 2, 2011 at 13:42
  • 1
    To avoid finding tags by mistake, and to be more specific about whether the branch is local or remote, you can specify git show-branch refs/heads/[branch] or git show-branch refs/remotes/origin/[branch]. Mar 16, 2018 at 20:42
11

git branch -l <branch-name>

returns the branch name if the branch exists and an nothing if it not exists

1
  • 2
    This is a better solution. e.g: git_checkout_main() { main_branch=$(git branch -l main) if [ -z "${main_branch}" ]; then git checkout master else git checkout main fi }
    – Viet
    Jan 14 at 15:37
4

On windows batch script it is bit different,

git rev-parse --verify <branch>

if %ERRORLEVEL% == 0  (
    echo "Yes"
) else (
    echo "No"
)
1
  • Will also return Yes for tags with same name
    – damkrat
    Jun 15, 2022 at 11:20
3

Yup, there is one.

git rev-parse [<options>] <args>…​

See https://git-scm.com/docs/git-rev-parse where you can find the set of arguments and the function.

git rev-parse --verify <branch-name>
2

Let's call it git is_localbranch (you need to add alias in .gitconfig).

Usage:

$ git is_localbranch BRANCH

Source:

git branch | grep -w $1 > /dev/null
if [ $? = 0 ]
then
  echo "branch exists"
fi
1
  • If it's called git-is_localbranch and is available in any directory in the PATH, you don't need to alias it in .gitconfig: git <command> will seek a git-<command> executable, e.g. git foo will seek a git-foo executable.
    – Dereckson
    Sep 28, 2020 at 14:26
2

Neither git show-ref nor git rev-parse works on my case.

$ git --version
git version 2.21.0

$ git show-branch --list
* [master] mybranch commit

$ BRANCH_NAME=mybranch
$ git rev-parse --verify $BRANCH_NAME
fatal: Needed a single revision

$ git show-ref refs/heads/$BRANCH_NAME
<no otput>
$ [ $? == 0 ] && echo "$BRANCH_NAME exists" || echo "$BRANCH_NAME not exists"
mybranch not exists

I ended up with this

$ BRANCH_NAME=mybranch
$ SHOW_ALL=`git show-branch --all | grep -w $BRANCH_NAME`
$ [ $? == 0 ] && echo "$BRANCH_NAME exists" || echo "$BRANCH_NAME not exists"
mybranch exists

You can do also with a script file

#!/bin/sh
BRANCH_NAME=mybranch
if grep -Fqe $BRANCH_NAME << EOF
`git show-branch --all`
EOF
then
   echo "$BRANCH_NAME exists"
else
   echo "$BRANCH_NAME not exists"
fi
2
  • 1
    the problem with grepping for just the branch name is you might have two branches with that name as a subset of the branch name. Even with the -w flag to grep, you could have branches mybranch, v2-mybranch, conrad-mybranch, etc Mar 23, 2021 at 20:25
  • This is pretty old at this point, but I'll note that your git show-branch output shows that you have exactly one (local) branch named master. The current commit on your master branch has, as its commit subject, mybranch commit. This does not mean that a branch named mybranch exists.
    – torek
    Aug 20, 2021 at 5:38
1

The outcome of review on my 'Suggested Edit' to the 'Update' on initial question was 'It should have been written as a comment or an answer', so I'm posting it here:

The another way proposed will not only verify branches but any reference with such name @jhuynh.

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

Issue with an 'Update' on initial quiestion explained:

Lets assume and check that 'master.000' is only a tag, such local branch does not exist, grep returns one entry wchich is a tag. Still rev-parse will return 0 if reference exists, even if such local branch does not exist. This is a false match, exactly as mentioned by @paul-s

$ git show-ref |grep master.000

f0686b8c16401be87e72f9466083d29295b86f4a refs/tags/master.000
$ git rev-parse --verify master.000
f0686b8c16401be87e72f9466083d29295b86f4a
$ echo $?
0
1
git show-branch <BRANCH-NAME> &>/dev/null && echo yes || echo no
0

This is how I implemented it, looks more stable I think

$branchExists = git ls-remote $gitUrl $gitBranch
if(!([bool]$branchExists))
{
   Write-Host "branch $branchName does not exist"  
} 
else
{
   Write-Host "branch $branchName exists"         
}

If the branch exists the content of the branchExist variable would be something like:

hashcode of branch name         feature/my_branch
0

To verify if a branch exists on remote, this worked fine to me:

git branch -r | grep -qn origin/${GIT_BRANCH_NAME}$ && echo "branch exists" || echo "branch does not exists"
-1

For use in a script, I recommend the following command:

git ls-remote --heads <repo_url> "<branch_name>" | wc -l

Note that <repo_url> can just be a "." to specify the local repo if you are inside its directory structure, the path to a local repo, or the address of a remote repo.

The command returns a 0 if the <branch_name> is not present of 1 if present.

-1
git branch --list $branch_name | grep $branch_name

then check the return value is 0 or 1.

1
  • Tried this, It will return 0 everytime. You could do git branch -a | grep remotes/origin/Catalog_3.2.3.07 | wc -l
    – NiharGht
    Apr 26, 2021 at 16:19
-2

If you can manage to include grep.

git branch | grep -q <branch>
2
  • 1
    That might give you the wrong answer if you use dots (".") in branch names, like I sometimes do, since the dot is interpreted by grep as a metacharacter. Feb 17, 2015 at 8:17
  • 1
    You will also get a false positive if you test for a branch whose name is a substring of a real branch. e.g. abc will match if there is a branch called abcdef.
    – rjmunro
    Sep 25, 2015 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.