How to detect if a branch exists using git command?
Need this in some shell script
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With