Extract latest tag version from Git in V1.0.0 format

Issue

I want to extract the number of latest version of a Git repository. This what I’ve done so far:

#--- Checking out latest tag
printf "%bChecking out tag...%b\n" "${YELLOW}" "${GRAY}"
LATEST_TAG=$(git describe --tags)
printf "\nChecking out tag %b${LATEST_TAG}%b\n" "${YELLOW}" "${GRAY}"
git checkout -b V${LATEST_TAG%-*} ${LATEST_TAG}

My problem is that if git describe --tags returns something like this 1.0.0-39-gf8f8306 I end up creating a branch V1.0.0-39.

What I want is to create a branch named V1.0.0.

Solution

It’s a bit of a secret,* but you can use --abbrev=0:

git describe --tags --abbrev=0

*git help describe mentions it, but you have to know where to look:

--abbrev=<n>

[…] An <n> of 0 will suppress long format, only showing the closest tag.

Answered By – Biffen

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published