You can use git show-ref :
git show-ref
If it is empty, it is SHA1 (or an invalid object, which is not very good).
If not, this is the name of the branch.
The best method comes from Validate if commit exists "using git merge-base :
The branch name will lead to another line (SHA1)
C:\Users\vonc\prog\b2d>git merge-base master master de4accfd28c5f25fcc057d56996b83450be5dc60
SHA1 will produce the same result (or at least start with the same result ):
C:\Users\vonc\prog\b2d>git merge-base 03949c3d3f88a378c6a08e57daa97059b52813f1 03949c3d3f88a378c6a08e57daa97059b52813f1 03949c3d3f88a378c6a08e57daa97059b52813f1
foobar will fail:
C:\Users\vonc\prog\b2d>git merge-base xxx xxx fatal: Not a valid object name xxx
This means something like:
if [[ git merge-base $string $string ]]; then if [[ $(git merge-base $string $string) == $string* ]]; then echo "SHA1" else echo "branch" fi else echo "Not a valid object name '$string'" fi
source share