Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a branch exists using git command? [duplicate]

Tags:

git

branch

How to detect if a branch exists using git command?

Need this in some shell script

like image 814
DeepNightTwo Avatar asked Nov 14 '25 17:11

DeepNightTwo


1 Answers

To check whether the string in the shell variable $REVISION correctly resolves to a valid object in git's database, you can use

git rev-parse --quiet --verify $REVISION

This will print the SHA1 to stdout if the revision is found, and return with a non-zero exit status otherwise, so you can use it in if clauses:

if git rev-parse --quiet --verify $REVISION > /dev/null; then
    # whatever
fi

Note that this will not only allow branch names in the strict sense, but all valid revision references.

like image 152
Sven Marnach Avatar answered Nov 17 '25 08:11

Sven Marnach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!