Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@Steve973

This comment has been minimized.

Copy link

@Steve973 Steve973 commented Sep 6, 2017

Thank you for this. I searched google to see how to find the latest tag on a branch. Your link was the first one I clicked on, and your explanation worked perfectly! Cheers!

@yuvaraj123

This comment has been minimized.

Copy link

@yuvaraj123 yuvaraj123 commented May 21, 2018

Works at first attempt..Thanks

@Inkognitoo

This comment has been minimized.

Copy link

@Inkognitoo Inkognitoo commented Jul 11, 2018

Thank you!

@dchorian

This comment has been minimized.

Copy link

@dchorian dchorian commented Jul 13, 2018

Exactly what I was looking for! Thanks!

@lstomberg

This comment has been minimized.

Copy link

@lstomberg lstomberg commented Aug 23, 2018

Thanks for this! Well documented.

@arekm

This comment has been minimized.

Copy link

@arekm arekm commented Aug 25, 2018

But how to do that without checkouting repository? There is "git ls-remote --tags --sort="v:refname" git://github.com/git/git.git" but no way to sort by tag creation date it seems.

@coleHafner

This comment has been minimized.

Copy link

@coleHafner coleHafner commented Feb 8, 2019

Works great for me. Thank you!

@KiltedCode

This comment has been minimized.

Copy link

@KiltedCode KiltedCode commented Mar 27, 2019

Thank you for this! We were having issues where for some reason git describe --abbrev=0 --tags was no longer returning the appropriate latest annotated tag, but the last example works.

@ppetpadriew

This comment has been minimized.

Copy link

@ppetpadriew ppetpadriew commented Jun 3, 2019

Thank man!

@stormmore

This comment has been minimized.

Copy link

@stormmore stormmore commented Sep 2, 2019

But how to do that without checkouting repository? There is "git ls-remote --tags --sort="v:refname" git://github.com/git/git.git" but no way to sort by tag creation date it seems.

instead of --sort="v:refname", use --sort=taggerdate or --sort=-taggerdate depending on the ascending or descending requirement. That said, probably will only work on annotated tags.

@aryehbeitz

This comment has been minimized.

Copy link

@aryehbeitz aryehbeitz commented Sep 8, 2019

this did it better.

git tag | sort -V | tail -1
@Ghost---Shadow

This comment has been minimized.

Copy link

@Ghost---Shadow Ghost---Shadow commented Sep 20, 2019

this did it better.

git tag | sort -V | tail -1

This does not work

0.1.9 should come before 0.1.10

The sort just sorts alphabetically

@tavo-sanchez

This comment has been minimized.

Copy link

@tavo-sanchez tavo-sanchez commented Sep 26, 2019

This does not work for me either. I have a list of tags like the one below

$git tag
v1.1.1
v1.1.2
v1.1.3
v1.2.0-rc.1
v1.2.0-rc.2
v1.2.0-rc.3
v1.2.0-rc.4
v1.2.0-rc.5
v1.2.0-rc.6
v1.2.0-rc.7

The tags RC are generated automatically, then I am tagging manually for the release version -> git tag v1.2.0 and doing a git describe I am expecting the v1.2.0 since it is the tag I just added. However, I am getting the v1.2.0-rc.7

Any idea why?

$git describe
v1.2.0-rc.7

$git tag
v0.1.0-rc.1
v0.1.0-rc.2
v0.1.0-rc.3
v1.0.0
v1.1.0
v1.1.0-rc.1
v1.1.1
v1.1.2
v1.1.3
v1.2.0           <- here is the lastest tag I added. As per the git documentation it lists the tags in alphabetical order which makes senses
v1.2.0-rc.1
v1.2.0-rc.2
v1.2.0-rc.3
v1.2.0-rc.4
v1.2.0-rc.5
v1.2.0-rc.6
v1.2.0-rc.7

The tag v1.2.0-rc.7 and v1.2.0 are on the same commit

@sczerwinski

This comment has been minimized.

Copy link

@sczerwinski sczerwinski commented Oct 22, 2019

@tavo-sanchez
That's exactly because both tags are on the same commit.
git rev-list --tags --max-count=1 gets the commit ID for the latest tag, and then git describe --tags [COMMIT_ID] displays the tags on this commit.

@patricknelson

This comment has been minimized.

Copy link

@patricknelson patricknelson commented Oct 24, 2019

Yeah, this is tricky. To get the “latest,” you really have to define what you actually mean by “latest,” for example:

  • Most recent in time? Add --sort=committerdate flag.
  • Largest version number? Pipe through | sort -V (for version friendly sorting)

This is important, because in some repositories, depending on the branching structure and amount of concurrent supported major versions, you may easily have a version tag like 1.2.3 added chronologically after version 2.3.4. By default, git will simply sort tags lexicographically (er, alphabetical) and that can even be changed based on your user settings.

Ideally, you just wanna sort consistently, across machines and developers for a consistent experience (e.g. like for tagging and shipping builds). For example:

# Most recent by date
git tag --sort=committerdate | tail -1

# Largest version number (assuming ALL your tags are numbered... see below)
git tag | sort -V

In my case, I prefer to filter out tags that start with a number (my system is 1.2.3, but your system could be v1.2.3) because I will often work in repositories that may contain non-numbered/non-version tags (which will become clutter for versioning). So depending on your needs, that would just become:

# Pure number, e.g. 1.2.3
git tag --sort=committerdate | grep -E '^[0-9]' | tail -1
git tag  | grep -E '^[0-9]' | sort -V | tail -1

# Or with v-prefix, e.g. v1.2.3
git tag --sort=committerdate | grep -E '^v[0-9]' | tail -1
git tag  | grep -E '^v[0-9]' | sort -V | tail -1

# Don’t forget: You can even use -v flag in grep to exclude! E.g.
git tag  | grep -E '^v[0-9]' | grep -v '-rc' | sort -V | tail -1

Hopefully this helps someone else. I know I studied and wrote most of this up for my own reference too! 😎

@falnyr

This comment has been minimized.

Copy link

@falnyr falnyr commented Oct 24, 2019

@patricknelson beautiful, thanks! 👌

@nfantone

This comment has been minimized.

Copy link

@nfantone nfantone commented Dec 26, 2019

Adding on top of @patricknelson excellent response, you can omit grep entirely by providing a pattern to git tag.

# Equivalent to grep -E '^[0-9]'
git tag --sort=committerdate --list '[0-9]*'

Additionally, if you are on PowerShell, you may want to replace tail -1 with Select-Object -Last 1 to fetch that last item.

# Equivalent to tail -1
git tag --sort=v:refname | Select-Object -Last 1
@tavo-sanchez

This comment has been minimized.

Copy link

@tavo-sanchez tavo-sanchez commented Feb 7, 2020

@patricknelson great explanation, thanks!

@nfantone nice! Thank you!

@ispyhumanfly

This comment has been minimized.

Copy link

@ispyhumanfly ispyhumanfly commented Feb 18, 2020

This is how you get the latest tagged version, but remove the v...

git tag --sort=committerdate | grep -E '[0-9]' | tail -1 | cut -b 2-7
@remysalim

This comment has been minimized.

Copy link

@remysalim remysalim commented Feb 26, 2020

I had more accurate results with taggerdate as a sort key instead of committerdate
git tag --sort=taggerdate | tail -1

@thomasleduc

This comment has been minimized.

Copy link

@thomasleduc thomasleduc commented Mar 25, 2020

I had more accurate results with taggerdate as a sort key instead of committerdate
git tag --sort=taggerdate | tail -1

Yes or the creatordate, the committerdate doesn't work for me.
And don't forget that you can add - (e.g. --sort=-taggerdate) before to sort it as newer -> older

@the-glima

This comment has been minimized.

Copy link

@the-glima the-glima commented Apr 8, 2020

For the latest tag on remote I'm doing this:

$ git ls-remote --tags --sort=committerdate | grep -o 'v.*' | sort -r

v1.2.3
v1.2.2
v1.2.1
v1.2.0
v1.1.0

Or, what I wanted, just the single latest one:

 $ git ls-remote --tags --sort=committerdate | grep -o 'v.*' | sort -r | head -1

v1.2.3
@bodia-uz

This comment has been minimized.

Copy link

@bodia-uz bodia-uz commented Apr 9, 2020

Faced with problem after using taggerdate and old git version on CI:

git tag --list "RC;*" --sort=taggerdate | tail -1
error: unsupported sort specification ‘taggerdate’

Found a better way to do it (git describe and --abbrev=0 argument):

git describe --abbrev=0
git describe --abbrev=0 --match "RC;*"

You can find a same example on https://git-scm.com/docs/git-describe page:
image

The difference is, that git will take the latest tag for this branch (not for all branches).

A similar approach uses lerna for getting the last tag:
https://github.com/lerna/lerna/blob/f2c3a92fe41b6fdc5d11269f0f2c3e27761b4c85/utils/collect-updates/collect-updates.js#L31
https://github.com/lerna/lerna/blob/f2c3a92fe41b6fdc5d11269f0f2c3e27761b4c85/utils/describe-ref/lib/describe-ref.js#L36

@joseluisq

This comment has been minimized.

Copy link

@joseluisq joseluisq commented Apr 23, 2020

In my case (Git v2.25), the command git describe --tags $(git rev-list --tags --max-count=1) returns the latest tag.

@varundey

This comment has been minimized.

Copy link

@varundey varundey commented May 4, 2020

What's wrong with a simple git tag -l | tail -1?

@remysalim

This comment has been minimized.

Copy link

@remysalim remysalim commented May 4, 2020

It's sorted alphabetically...

@tsologub

This comment has been minimized.

Copy link

@tsologub tsologub commented Sep 19, 2020

+1 to @joseluisq. Thx man!

@brunocoelho

This comment has been minimized.

Copy link

@brunocoelho brunocoelho commented Oct 2, 2020

Just in case anyone is looking for how to update package.json's version with the latest tag:

let fs = require('fs');
const { exec, execSync } = require('child_process');

if (fs.existsSync('./package.json')) {
  var package = require('./package.json');
  let currentVersion = package.version;

  exec('git tag --sort=committerdate | tail -1', (error, stdout) => {
    if (error) {
      console.error(`exec error: ${error}`);
      return;
    }

    let newVersion = stdout.trim();
    package.version = newVersion;
    fs.writeFileSync('./package.json', JSON.stringify(package, null, 2));

    execSync("git commit -am 'Bump version '" + newVersion);

    console.log('Version updated', currentVersion, '=>', newVersion);
  });
}
@Mr-LiuDC

This comment has been minimized.

Copy link

@Mr-LiuDC Mr-LiuDC commented Oct 12, 2020

Not working with gitlab, got the usage information.

$ git rev-list --tags --max-count=1
usage: git rev-list [OPTION] <commit-id>... [ -- paths... ]
  limiting output:
    --max-count=<n>
    --max-age=<epoch>
    --min-age=<epoch>
    --sparse
    --no-merges
    --min-parents=<n>
    --no-min-parents
    --max-parents=<n>
    --no-max-parents
    --remove-empty
    --all
    --branches
    --tags
    --remotes
    --stdin
    --quiet
  ordering output:
    --topo-order
    --date-order
    --reverse
  formatting output:
    --parents
    --children
    --objects | --objects-edge
    --unpacked
    --header | --pretty
    --abbrev=<n> | --no-abbrev
    --abbrev-commit
    --left-right
  special purpose:
    --bisect
    --bisect-vars
    --bisect-all
@cawa-93

This comment has been minimized.

Copy link

@cawa-93 cawa-93 commented Dec 11, 2020

Maybe someone will tell me how to get the last tag, or if there are no tags in the branch - the initial commit?

@nextgenthemes

This comment has been minimized.

Copy link

@nextgenthemes nextgenthemes commented Jan 30, 2021

I would like to know how the last tag that was tagged before a specific tag. Meaning lets say I have.

2.0.0
1.5.10
1.5.8
1.0.1

I like to say git --last-tag-b4="1.5.10" and it should give me 1.5.8. When 1.5.9 was never created or deleted.

@patricknelson

This comment has been minimized.

Copy link

@patricknelson patricknelson commented Jan 30, 2021

@cawa-93 why not just git tag | sort -V | tail -1? i.e. Sort tags by version and just take the last one (tail -1). If that's empty, then git tag returned nothing.

@nextgenthemes The best (hacky) way I can think of quickly is to use version sort and then use grep to to find an exact match for the version you know already exists (a possible shortcoming if that exact version you're looking for isn't already present).

# 1. Pull out tags
# 2. Use version sort
# 3. Use grep to find the known version, ensuring we also output the line before
# 4. Just take the first line (the line before the match)

git tag | sort -V | grep -B 1 1.5.8 | head -1

Alternative, you could probably use awk to use a comparison operator (e.g. awk '$1 < NUMBER { print }') but I don't know how to do that when the number has multiple dots in it, nor in a way that would be version safe... 😞

@nextgenthemes

This comment has been minimized.

Copy link

@nextgenthemes nextgenthemes commented Jan 30, 2021

@patricknelson Thanks. I was thinking about some pipe thing. This actually reads and works quite nice. Not sure why I am obsessing trying to do it with more git and less gnu right now. Tell me to stop ;)

@dimisjim

This comment has been minimized.

Copy link

@dimisjim dimisjim commented Mar 16, 2021

One could also do, without using git: curl -s GET https://api.github.com/repos/aws/aws-cli/tags | jq -r '.[].name' | head -n1

@cloverzrg

This comment has been minimized.

Copy link

@cloverzrg cloverzrg commented Mar 19, 2021

One could also do, without using git: curl -s GET https://api.github.com/repos/aws/aws-cli/tags | jq -r '.[].name' | head -n1

add per_page params curl -s GET https://api.github.com/repos/cloudreve/Cloudreve/tags\?per_page\=1 | jq -r '.[].name'

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