Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the second most recent git tag

Tags:

I need the sha of the second most recent git tag. When running

git show-ref --tags

i get

ab295f707bc42e8975fd4d87142ca436c0fac94f refs/tags/initial-commit-tag
1c816fc0895faeecd48b9b31e09ba825698f92a6 refs/tags/production-2018-03-06-001

which shows my two tags i currently have. How can i get it to output just ab295f707bc42e8975fd4d87142ca436c0fac94f?

I tried git rev-list --tags --max-count=1 --skip=1 but that apparently includes non tags as well, because when i run

git rev-list --tags --max-count=30 --skip=1

i get

d3d536c6745c823994b3fec8ce3b26caf2b4c6f5
08278f196cad30b5acbdd9d5f0baa58baab630bc
480513c6d94c6df52f49b91c5f90c39af4fc8f34
183a91321a85083fdf29b77bcbce5b9ca3932f12
e04e120be2f200bae88de37d1be2cebc8b07f74f
168b696f915adaea95c4e0d2a8cea68ea25538b2
0b9aa10a4f005f349e9553ac75552abc07723b95
befa7507836af93b1c765d957c65cbcf6ebea890
6850539077d82847907410d587ca3da047043915
a0a6d7b98c64ad318ff0d2c4252ac1ad94959bb2
3e91ed05db57b5dec23df0a465b9c8bc0a8dfe26
2341cf5a58974a9f57b0aa92a837e7857631f5cb
385a88d22c77cf26f21a906a2be65695528960b9
c7548f04d7b928eb7bf2f485aeb6fa78ef6846e9
a5abb21b1acb59617e3d69173d883c3e5f7db0f2
b0595fae8c8f697aa02dbd49d149da4f4efabad6
2c08455a1b86e1ba6de86844a83346262ab89e00

so there are not only tags in this list. How can i filter for just tags in here/make the first command workable to only show the second recent one?

Note: am on Mac/Linux, so bash piping would work as well.

EDIT:

In the end the --no-walk option wordked for me, so

git rev-list --tags --max-count=1 --skip=1 --no-walk

The accepted answer below is much more powerful though.

like image 608
Johannes Merz Avatar asked Mar 06 '18 14:03

Johannes Merz


People also ask

How do I get the most recent tags in git?

In order to find the latest Git tag available on your repository, you have to use the “git describe” command with the “–tags” option. This way, you will be presented with the tag that is associated with the latest commit of your current checked out branch.

How do I checkout to new tag?

In order to checkout the latest Git tag, first update your repository by fetching the remote tags available. As you can see, you retrieve multiple tags from your remote repository. Then, retrieve the latest tag available by using the “git describe” command.

How do I tag last commit?

In order to create a Git tag for the last commit of your current checked out branch, use the “git tag” command with the tag name and specify “HEAD” as the commit to create the tag from. Similarly, if you want your tag to be annotated, you can still use the “-a” and “-m” options to annotate your tag.

Can you have multiple tags in git?

To push multiple tags simultaneously pass the --tags option to git push command. When another user clones or pulls a repo they will receive the new tags.


1 Answers

You could refactor a bit your git command and pipe it to sed to extract the data you need :

git for-each-ref --sort=-taggerdate --format '%(objectname)' refs/tags | sed -n 2p  

Explanation:

  • git for-each-ref : documentation link. Displays information about refs.
    • --sort=-taggerdate : reverse sort the output by tag date
    • --format '%(objectname)' : we only output the hash
    • refs/tags: we only list the tags in refs/tags
  • sed -n 2p: output only the 2nd line

Note that if you don't have a taggerdate on your tags, you can use other sort options, like creatordate (full list of available fields here).

like image 55
Aserre Avatar answered Sep 23 '22 12:09

Aserre