Home > Blockchain >  git latest tag from local repository
git latest tag from local repository

Time:11-18

This works on my local machine

git ls-remote --tags | grep -o 'refs/tags/[0-9]*\.[0-9]*' | sort -r | head -1 | grep -o '[^\/]*$

but not in my jenkins build server which is running from docker, it doesn't have the rights and I can't seem to fix that.

Is there an alternative for ls-remote which would give me exactly the same output, but then for my local git repository?

Or, is there a silver bullet solution for getting the LATEST tag from my local repo, looking from the tip of the branch and then backwards? I have been struggling with git tag | head -1 and all kinds of alternatives but nothing gives me the latest tag searching back from the tip of the branch....

CodePudding user response:

It looks like you are trying to compare version numbers.

If you are interested in "the tag with the highest version number in my repo" :

git tag --sort=v:refname | tail -1

if your version tags have a pattern to distinguish them from other tags :

git tag --sort=v:refname --list "v[0-9]*" | tail -1

note that the filter is a glob pattern, not a regexp :

v[0-9]* in the example above means : v followed by any char in [0-9] followed by anything else (*)


This will work if you have all tags locally, so run git fetch --tags before that to have an up to date list.


If you want "the tag with the highest version number in the history of my current branch" :

git tag --sort=v:refname --merged=HEAD | tail -1

If you want "the tag closest to my current commit" :

git describe --tags --abbrev=0

CodePudding user response:

git describe is a native way, considering

With --abbrev set to 0, the command can be used to find the closest tagname without any suffix

  •  Tags:  
  • git
  • Related