如何在 Git 的当前分支中获取最新的标签名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1404796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the latest tag name in current branch in Git?
提问by culebrón
What's the simplest way to get the most recent tag in Git?
在 Git 中获取最新标签的最简单方法是什么?
git tag a HEAD
git tag b HEAD^^
git tag c HEAD^
git tag
output:
输出:
a
b
c
Should I write a script to get each tag's datetime and compare them?
我应该编写一个脚本来获取每个标签的日期时间并进行比较吗?
采纳答案by JB.
You could take a look at git describe
, which does something close to what you're asking.
你可以看看git describe
,它做的事情接近你的要求。
回答by acassis
To get the most recent tag:
获取最新标签:
git describe --tags
To get the most recent annotatedtag:
要获取最新的注释标签:
git describe --abbrev=0
回答by kilianc
Will output the tag of the latest tagged commit across all branches
将输出所有分支的最新标记提交的标记
git describe --tags $(git rev-list --tags --max-count=1)
回答by William Pursell
To get the most recent tag, you can do:
要获取最新的标签,您可以执行以下操作:
$ git for-each-ref refs/tags --sort=-taggerdate --format='%(refname)' --count=1
Of course, you can change the count argument or the sort field as desired. It appears that you may have meant to ask a slightly different question, but this does answer the question as I interpret it.
当然,您可以根据需要更改计数参数或排序字段。看起来你可能想问一个稍微不同的问题,但这确实回答了我解释的问题。
回答by Wincent Colaiuta
How about this?
这个怎么样?
TAG=$(git describe $(git rev-list --tags --max-count=1))
TAG=$(git describe $(git rev-list --tags --max-count=1))
Technically, won't necessarily get you the latest tag, but the latest commit which is tagged, which may or may not be the thing you're looking for.
从技术上讲,不一定会为您提供最新的标签,而是带标签的最新提交,这可能是您正在寻找的东西,也可能不是。
回答by xserrat
You can execute: git describe --tags $(git rev-list --tags --max-count=1)
talked here: How to get latest tag name?
你可以执行:git describe --tags $(git rev-list --tags --max-count=1)
talk here: How to get latest tag name?
回答by Matthew Brett
"Most recent" could have two meanings in terms of git.
就 git 而言,“最近”可能有两种含义。
You could mean, "which tag has the creation date latest in time", and most of the answers here are for that question. In terms of your question, you would want to return tag c
.
您可能的意思是“哪个标签的创建日期是最新的”,这里的大部分答案都是针对该问题的。就您的问题而言,您希望返回 tag c
。
Or you could mean "which tag is the closest in development history to some named branch", usually the branch you are on, HEAD
. In your question, this would return tag a
.
或者您可以指“哪个标签在开发历史中与某个命名分支最接近”,通常是您所在的分支HEAD
。在您的问题中,这将返回 tag a
。
These might be different of course:
这些当然可能不同:
A->B->C->D->E->F (HEAD)
\ \
\ X->Y->Z (v0.2)
P->Q (v0.1)
Imagine the developer tag'ed Z
as v0.2
on Monday, and then tag'ed Q
as v0.1
on Tuesday. v0.1
is the more recent, but v0.2
is closer in development history to HEAD, in the sense that the path it is on starts at a point closer to HEAD.
想象一下开发人员标记Z
为v0.2
星期一,然后标记Q
为v0.1
星期二。 v0.1
是最近的,但v0.2
在开发历史上更接近 HEAD,因为它所走的道路始于更接近 HEAD 的点。
I think you usually want this second answer, closer in development history. You can find that out by using git log v0.2..HEAD
etc for each tag. This gives you the number of commits on HEAD since the path ending at v0.2
diverged from the path followed by HEAD.
我想你通常想要第二个答案,在发展历史上更接近。您可以通过git log v0.2..HEAD
对每个标签使用etc来发现这一点。这为您提供了 HEAD 上的提交次数,因为以 结尾v0.2
的路径与 HEAD 后跟的路径不同。
Here's a Python script that does that by iterating through all the tags running this check, and then printing out the tag with fewest commits on HEAD since the tag path diverged:
这是一个 Python 脚本,它通过遍历运行此检查的所有标签来执行此操作,然后打印出由于标签路径不同而在 HEAD 上提交次数最少的标签:
https://github.com/MacPython/terryfy/blob/master/git-closest-tag
https://github.com/MacPython/terryfy/blob/master/git-closest-tag
git describe
does something slightly different, in that it tracks back from (e.g.) HEAD to find the first tag that is on a path back in the history from HEAD. In git terms, git describe
looks for tags that are "reachable" from HEAD. It will therefore not find tags like v0.2
that are not on the path back from HEAD, but a path that diverged from there.
git describe
做了一些稍微不同的事情,因为它从(例如)HEAD 回溯以找到在从 HEAD 返回的历史路径上的第一个标签。在 git 术语中,git describe
查找可从 HEAD“访问”的标签。因此,它不会找到v0.2
不在从 HEAD 返回的路径上的标签,而是从那里发散的路径。
回答by beenhere4hours
git describe --tags
git describe --tags
returns the last tag able to be seen by current branch
返回当前分支能够看到的最后一个标签
回答by east
git log --tags --no-walk --pretty="format:%d" | sed 2q | sed 's/[()]//g' | sed s/,[^,]*$// | sed 's ...... '
IF YOU NEED MORE THAN ONE LAST TAG
如果您需要不止一个标签
(git describe --tags sometimes gives wrong hashes, i dont know why, but for me --max-count 2 doesnt work)
(git describe --tags 有时会给出错误的哈希值,我不知道为什么,但对我来说 --max-count 2 不起作用)
this is how you can get list with latest 2 tag names in reverse chronological order, works perfectly on git 1.8.4. For earlier versions of git(like 1.7.*), there is no "tag: " string in output - just delete last sed call
这是您如何按时间倒序获取最新 2 个标签名称的列表,在 git 1.8.4 上完美运行。对于早期版本的 git(如 1.7.*),输出中没有“tag:”字符串 - 只需删除最后一个 sed 调用
If you want more than 2 latest tags - change this "sed 2q" to "sed 5q" or whatever you need
如果您需要 2 个以上的最新标签 - 将此“sed 2q”更改为“sed 5q”或您需要的任何标签
Then you can easily parse every tag name to variable or so.
然后您可以轻松地将每个标签名称解析为变量左右。
回答by gavenkoa
What is wrongwith all suggestions (except Matthew Brettexplanation, up to date of this answer post)?
所有建议都有什么问题(除了Matthew Brett 的解释,这个回答帖子的最新版本)?
Just run any command supplied by other on jQuery Git history when you at different point of historyand check result with visual tagging history representation(I didthat is why you see this post):
当您处于不同的历史记录点时,只需在 jQuery Git 历史记录上运行其他人提供的任何命令,并使用可视化标记历史记录表示检查结果(我这样做就是您看到这篇文章的原因):
$ git log --graph --all --decorate --oneline --simplify-by-decoration
Todays many project perform releases (and so tagging) in separate branch from mainline.
今天,许多项目在与 mainline 不同的分支中执行发布(以及标记)。
There are strong reasonfor this. Just look to any well established JS/CSS projects. For user conventions they carry binary/minified release files in DVCS. Naturally as project maintainer you don't want to garbage your mainline diffhistory with useless binary blobs and perform commit of build artifacts out of mainline.
这是有充分理由的。只需查看任何完善的 JS/CSS 项目即可。对于用户约定,他们在 DVCS 中携带二进制/缩小的发布文件。当然作为项目的维护者,你不希望你的垃圾主线DIFF与无用的二进制块病史并进行构建工件犯了主线。
Because Git uses DAG and not linear history - it is hard to define distance metricso we can say - oh that rev is most nearest to my HEAD
!
因为 Git 使用 DAG 而不是线性历史 -很难定义距离度量,所以我们可以说 - 哦,rev 最接近我的HEAD
!
I start my own journey in (look inside, I didn't copy fancy proof images to this long post):
我开始了我自己的旅程(看看里面,我没有将花哨的证明图片复制到这篇长篇文章中):
What is nearest tag in the past with respect to branching in Git?
Currently I have 4 reasonable definition of distance between tag and revision with decreasing of usefulness:
目前我有 4 个合理的标签和修订之间的距离定义,随着实用性的降低:
- length of shortest pathfrom
HEAD
to merge basewith tag - dateof merge basebetween
HEAD
and tag - number of revsthat reachable from HEAD but not reachable from tag
- dateof tag regardless merge base
- 最短路径的长度从
HEAD
到合并基础与标签 - 和标签之间的合并基准日期
HEAD
- 可从 HEAD 访问但无法从标签访问的转数
- 不考虑合并基础的标签日期
I don't know how to calculate length of shortest path.
我不知道如何计算最短路径的长度。
Script that sort tags according to dateof merge basebetween HEAD
and tag:
根据和标签之间的合并基准日期对标签进行排序的脚本 :HEAD
$ git tag \
| while read t; do \
b=`git merge-base HEAD $t`; \
echo `git log -n 1 $b --format=%ai` $t; \
done | sort
It usable on most of projects.
它可用于大多数项目。
Script that sort tags according to number of revsthat reachable from HEAD but not reachable from tag:
根据从 HEAD 可到达但从标签不可到达的转数对标签进行排序的脚本:
$ git tag \
| while read t; do echo `git rev-list --count $t..HEAD` $t; done \
| sort -n
If your project history have strange dates on commits (because of rebases or another history rewriting or some moron forget to replace BIOS battery or other magics that you do on history) use above script.
如果你的项目历史在提交时有奇怪的日期(因为 rebase 或其他历史重写,或者一些白痴忘记更换 BIOS 电池或其他你在历史上做的魔术)使用上面的脚本。
For last option (dateof tag regardless merge base) to get list of tags sorted by date use:
对于最后一个选项(日期标签,无论合并基础),以获得按日期排序使用标签列表:
$ git log --tags --simplify-by-decoration --pretty="format:%ci %d" | sort -r
To get known current revision date use:
要获得已知的当前修订日期,请使用:
$ git log --max-count=1
Note that git describe --tags
have usage on its own cases but not for finding human expected nearest tag in project history.
请注意,git describe --tags
在其自己的情况下使用,但不适用于在项目历史中查找人类预期的最近标签。
NOTEYou can use above recipes on any revision, just replace HEAD
with what you want!
注意您可以在任何版本中使用上述配方,只需替换HEAD
为您想要的即可!