I have to get the latest git tag from the remote git repository. I have used following command for finding the latest tag
git ls-remote --tags [email protected]:xxxx.git |grep "\."|grep -v -|grep -v {| sort -n -t. -k3 -k4
This gives me following output
c8be4313ae8261214acb6d3d41f9ece8d47a4ad5 refs/tags/v0.2.1
9e776cff51a8bb15f0539b852a819723d1e37c69 refs/tags/v0.2.2
ee1f173f4e7da0996af9f7c91e0952bec8c2358b refs/tags/v0.1.3
5d6777bf2b2e5bae41ae9ab966320c691c1f2ee2 refs/tags/v0.1.4
6d3040673330ed763bc0c1a6e6cd5dbc82392d4f refs/tags/v0.1.5
4afd29dc48805053be911a85c6da6b195e96e786 refs/tags/v0.1.6
8d5cc76d50b153f836561bb933b1f5ad488748d1 refs/tags/v0.1.7
1c0cdebaed828aaef2897c9240b4440898f70766 refs/tags/v0.1.8
683de590ba8d633c801d2628f4d9de58f9de371a refs/tags/v0.1.9
925797f07cfc94a3af1f56cdabd856e11b222b78 refs/tags/v0.1.10
But I have to find the v0.2.2 which is latest created. how can I find the latest created tag ( tag with latest created date) . Is there any other way to do it?
get-latest-tag-on-git.sh # 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. # and the abbreviated object name of the most recent commit.
Fetching changes from a remote repositoryUse git fetch to retrieve new work done by other people. Fetching from a repository grabs all the new remote-tracking branches and tags without merging those changes into your own branches. Otherwise, you can always add a new remote and then fetch.
Listing the available tags in Git is straightforward. Just type git tag (with optional -l or --list ). You can also search for tags that match a particular pattern. The command finds the most recent tag that is reachable from a commit.
You probably need latest tag reachable, not latest tag created:
git describe --tags --abbrev=0
Anyway, just in case you really need last tag created:
git does have two kinds of tags: lightweight and annotated. If your tags are lightweight then tough luck, you can't do this, creation date is not tracked. If you have access to filesystem where your remote repo is stored, then you can try checking timestamps on files in /refs/tags - but this info is not necessarily accurate (it's only timestamp of creation of tag file in this particular repo).
For annotated tags, however, you CAN obtain creation date; once you get sha (by ls-tree or other means), run:
git cat-file -p <sha>
to show tagger, message and creation date or simply:
git show <sha>
to additionally show referenced commit, too.
Using this information in script is doable (maybe not trivial due to date format).
I was going to refer you to git internals description about tags, but turns out, that all this info is also described on ProGit, Git Basics - Tagging.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With