简体   繁体   English

从远程git存储库中查找最新的git标记

[英]Find latest git tag from the remote git repository

I have to get the latest git tag from the remote git repository. 我必须从远程git存储库获取最新的git标记 I have used following command for finding the latest tag 我使用以下命令来查找最新的标签

git ls-remote --tags xxxxx@xxxx.xxxx.net: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. 但我必须找到最新创建的v0.2.2 how can I find the latest created tag ( tag with latest created date) . 如何找到最新创建的标签(带有最新创建日期的标签) Is there any other way to do it? 还有其他办法吗?

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. git有两种标签:轻量级和注释。 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). 如果您可以访问存储远程存储库的文件系统,那么您可以尝试检查/ refs / tags中文件的时间戳 - 但此信息不一定准确(它只是在此特定存储库中创建标记文件的时间戳)。

For annotated tags, however, you CAN obtain creation date; 但是,对于带注释的标签,您可以获得创建日期; once you get sha (by ls-tree or other means), run: 一旦你得到sha(通过ls-tree或其他方式),运行:

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 . 我打算引用你关于标签的git内部描述,但事实证明,所有这些信息也在ProGit,Git Basics - Tagging有所描述。

Assuming it's acceptable to determine this locally, you can make sure that your tags are up-to-date with: 假设在本地确定这一点是可以接受的,您可以确保您的标签是最新的:

git fetch --tags

And then run this from the command line: 然后从命令行运行它:

git rev-list --tags --timestamp --no-walk |
    sort -nr |
    head -n1 |
    cut -f 2 -d ' ' |
    xargs git describe --contains

The git rev-list command above with --tags will put down the commits for all tags, while the --timestamp will make sure each line is prefixed with the timestamp of the commit. 上面带有--tagsgit rev-list命令将放下所有标记的提交,而--timestamp将确保每一行都以提交的时间戳为前缀。 The --no-walk options tells rev-list not to list any parent commits, so the only commits that show up in the output are the ones tied to tags. --no-walk选项告诉rev-list不列出任何父提交,因此输出中显示的唯一提交是与标记绑定的提交。 sort -nr says sort the lines numerically and in reverse order so that the commit with the most recent timestamp is at the top. sort -nr表示以数字方式和相反顺序对行进行排序,以便最近时间戳的提交位于顶部。 The cut command allows us to peal off the commit id of the tagged commit, cut命令允许我们cut标记提交的提交ID,

Running this in my Python Nose repository, I get this: 在我的Python Nose存储库中运行它,我得到这个:

:: git rev-list --tags --timestamp --no-walk | sort -nr | head -n1 | cut -f 2 -d ' ' | xargs git describe --contains
release_1.3.0

If you must do this all remotely, then you don't really have the option of picking the one with the latest creation date. 如果您必须远程执行此操作,那么您实际上无法选择具有最新创建日期的那个。 That information is simply not advertised. 这些信息根本就没有公布。 You need to download the objects to your local repository first since all this metadata is captured in the objects. 您需要首先将对象下载到本地存储库,因为所有这些元数据都在对象中捕获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM