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

Git BranchGit

Git Branch Problem Overview


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. 

Git Branch Solutions


Solution 1 - Git Branch

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/"

Solution 2 - Git Branch

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

Solution 3 - Git Branch

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

Solution 4 - Git Branch

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

Solution 5 - Git Branch

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

Solution 6 - Git Branch

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.

Solution 7 - Git Branch

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>

Solution 8 - Git Branch

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

Solution 9 - Git Branch

On windows batch script it is bit different,

git rev-parse --verify <branch>

if %ERRORLEVEL% == 0  (
    echo "Yes"
) else (
    echo "No"
)

Solution 10 - Git Branch

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

Solution 11 - Git Branch

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

Solution 12 - Git Branch

git show-branch <BRANCH-NAME> &>/dev/null && echo yes || echo no

Solution 13 - Git Branch

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

Solution 14 - Git Branch

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"

Solution 15 - Git Branch

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.

Solution 16 - Git Branch

git branch --list $branch_name | grep $branch_name

then check the return value is 0 or 1.

Solution 17 - Git Branch

If you can manage to include grep.

git branch | grep -q <branch>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionManoj GovindanView Question on Stackoverflow
Solution 1 - Git BranchjhuynhView Answer on Stackoverflow
Solution 2 - Git BranchMark LongairView Answer on Stackoverflow
Solution 3 - Git BranchMartijnView Answer on Stackoverflow
Solution 4 - Git BranchRazzi AbuissaView Answer on Stackoverflow
Solution 5 - Git BranchTom HaleView Answer on Stackoverflow
Solution 6 - Git BranchMark DragoView Answer on Stackoverflow
Solution 7 - Git Branchuser11061050View Answer on Stackoverflow
Solution 8 - Git Branchuser1261322View Answer on Stackoverflow
Solution 9 - Git Branchpinkal vansiaView Answer on Stackoverflow
Solution 10 - Git BranchPshemy108View Answer on Stackoverflow
Solution 11 - Git BrancheQ19View Answer on Stackoverflow
Solution 12 - Git BranchVojtech VitekView Answer on Stackoverflow
Solution 13 - Git BranchShilanView Answer on Stackoverflow
Solution 14 - Git BranchLeonardo Torres MonteroView Answer on Stackoverflow
Solution 15 - Git BranchDavidView Answer on Stackoverflow
Solution 16 - Git BranchHowell ZHUView Answer on Stackoverflow
Solution 17 - Git BranchDaniel VikströmView Answer on Stackoverflow