Skip to content

Instantly share code, notes, and snippets.

@rponte
Last active July 4, 2024 10:55
Show Gist options
  • Save rponte/fdc0724dd984088606b0 to your computer and use it in GitHub Desktop.
Save rponte/fdc0724dd984088606b0 to your computer and use it in GitHub Desktop.
Getting latest tag on git repository
# The command finds the most recent tag that is reachable from a commit.
# If the tag points to the commit, then only the tag is shown.
# Otherwise, it suffixes the tag name with the number of additional commits on top of the tagged object
# and the abbreviated object name of the most recent commit.
git describe
# With --abbrev set to 0, the command can be used to find the closest tagname without any suffix:
git describe --abbrev=0
# other examples
git describe --abbrev=0 --tags # gets tag from current branch
git describe --tags `git rev-list --tags --max-count=1` # gets tags across all branches, not just the current branch
@TiloGit
Copy link

TiloGit commented Feb 8, 2023

git ls-remote --tags --sort=committerdate

@the-glima

important: (from https://git-scm.com/docs/git-ls-remote.html)
_but be aware keys like committerdate that require access to the objects themselves will not work for refs whose objects have not yet been fetched from the remote, and will give a missing object error.

I get this for example on:

git ls-remote --tags --sort=committerdate "https://github.com/box/box-java-sdk"
git ls-remote --tags --sort=committerdate "https://github.com/derailed/k9s"

Get:

tilo@mypc:/mnt/c/GHreps$ git ls-remote --tags --sort=committerdate "https://github.com/derailed/k9s"
fatal: missing object 25b364dd96ae53e70b9639a20732647fd4bb23a7 for refs/tags/0.1.0
tilo@mypc:/mnt/c/GHreps$ git ls-remote --tags --sort=committerdate "https://github.com/box/box-java-sdk"
fatal: missing object 9a8b39475952def867a5b68ab92e7cd45bb3d8e0 for refs/tags/v0.1

@eggbean
Copy link

eggbean commented Mar 20, 2023

If the remote is GitHub and there's an associated release for the tag, why not just use the GitHub API?

$ tag="$(curl -s https://api.github.com/repos/box/box-java-sdk/releases/latest | jq -r '.tag_name')"
$ echo $tag
v4.0.1

@nextgenthemes
Copy link

Just deleted a stupid long ass comment. Just want to warn about git tag --sort=committerdate it does not work correctly with the tag command for whatever reason. But this does work.

git for-each-ref --sort=creatordate --format '%(refname) %(creatordate)' refs/tags

COMPLETELY different result that makes no sense to me git tag --sort=committerdate

However git tag --sort=taggerdate is basically what I always looked for, what should be the default output for git-tag IMG. I also hate how it paginates the output with lessor whatever by default.

Again, be warned about committerdate ! There is somebody above that had issue as well I think because of this. Even if my commit of things I tagged days or maybe even a week later. I makes absolutely no sense to me WTF this produces. Maybe my memory is just bad and it all makes sense, well probably actually. I think to much about this shit.

@arderyp
Copy link

arderyp commented Jul 18, 2023

@eggbean I like that idea, but it hits an auth wall for private/enterprise repos

@eggbean
Copy link

eggbean commented Jul 18, 2023

@arderyp There are a few different ways to authenticate. Have you tried the gh cli tool?

@Gabomfim
Copy link

Gabomfim commented Dec 4, 2023

Thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment