如何在Git中获取当前分支的最新标签名称?
在 Git 中获取最新标签的最简单方法是什么?
git tag a HEAD
git tag b HEAD^^
git tag c HEAD^
git tag
输出:
a
b
c
我应该编写一个脚本来获取每个标签的日期时间并比较它们吗?
What's the simplest way to get the most recent tag in 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
获取最新的标签(随后的示例输出):
获取最新的标签,以及标记对象顶部的附加提交数量更多:
要获取最新的带注释标签:
To get the most recent tag (example output afterwards):
To get the most recent tag, with the number of additional commits on top of the tagged object & more:
To get the most recent annotated tag:
将输出所有分支上最新标记提交的标记
Will output the tag of the latest tagged commit across all branches
您可以看一下
gitdescribe
,它所做的事情与您所要求的很接近。You could take a look at
git describe
, which does something close to what you're asking.要获取最新的标签,您可以执行以下操作:
当然,您可以根据需要更改计数参数或排序字段。看来
您可能想问一个稍微不同的问题,但这确实回答了我所解释的问题。
To get the most recent tag, you can do:
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.
这个怎么样?
TAG=$(git describe $(git rev-list --tags --max-count=1))
从技术上讲,不一定会为您提供最新的标签,但会为您提供标记的最新提交,这可能是也可能不是您正在寻找的东西。
How about this?
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.
CI/CD 流程中
describe
的问题是您可能会遇到fatal: no Tags can describe
错误。之所以会发生这种情况,是因为根据
gitdescribe--help
:如果您想要存储库中的最新标签,无论您所在的分支是否可以访问该标签(通常是因为它不是当前分支树的一部分),此命令将为您提供整个存储库中最近创建的标签:
更多详细信息和其他排序键可以在这里找到: https://git -scm.com/docs/git-for-each-ref#_field_names
The problem with
describe
in CI/CD processes is you can run into thefatal: no tags can describe
error.This will occur because, per
git describe --help
:If you want the latest tag in the repo, regardless if the branch you are on can reach the tag, typically because it is not part of the current branch's tree, this command will give you the most recently created tag in the entire repo:
More details and other keys to sort on can be found here: https://git-scm.com/docs/git-for-each-ref#_field_names
如果您没有看到最新标签,请确保在运行之前获取来源:
If you don't see latest tag, make sure of fetching origin before running that:
您可以执行:
git describe --tags $(git rev-list --tags --max-count=1)
这里讨论:如何获取最新标签名称?You can execute:
git describe --tags $(git rev-list --tags --max-count=1)
talked here: How to get latest tag name?我不确定为什么问题所要求的内容没有答案。即所有标签(包括未注释的)且不带后缀:
I'm not sure why there are no answers to what the question is asking for. i.e. All tags (non-annotated included) and without the suffix:
“最新”在 git 中可能有两种含义。
您可能的意思是“哪个标签的创建日期最晚”,这里的大多数答案都是针对这个问题的。就您的问题而言,您希望返回标签
c
。或者你可能指的是“哪个标签在开发历史中最接近某个命名分支”,通常是你所在的分支,
HEAD
。在您的问题中,这将返回标签a
。当然,这些可能会有所不同:
想象一下开发人员在周一将
Z
标记为v0.2
,然后将Q
标记为 <周二代码>v0.1。v0.1
较新,但v0.2
在开发历史上更接近 HEAD,因为它所在的路径始于更接近 HEAD 的点。我想你通常想要第二个答案,更接近开发历史。您可以通过对每个标签使用
git log v0.2..HEAD
等来找到这一点。这为您提供了自以v0.2
结尾的路径与 HEAD 后面的路径分叉以来 HEAD 上的提交次数。下面是一个 Python 脚本,它通过迭代运行此检查的所有标签,然后打印出自标签路径分歧以来 HEAD 上提交次数最少的标签来实现此目的:
https://github.com/MacPython/terryfy/blob/master/git-closest-tag
git describe
做的事情略有不同,因为它从(例如)HEAD 回溯以找到在历史记录中从 HEAD 返回的第一个标签。在 git 术语中,gitdescribe
查找可从 HEAD“访问”的标签。因此,它不会找到像v0.2
这样的标签,这些标签不在从 HEAD 返回的路径上,而是从那里分叉的路径上。"Most recent" could have two meanings in terms of 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
.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 taga
.These might be different of course:
Imagine the developer tag'ed
Z
asv0.2
on Monday, and then tag'edQ
asv0.1
on Tuesday.v0.1
is the more recent, butv0.2
is closer in development history to HEAD, in the sense that the path it is on starts at a point closer to 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 atv0.2
diverged from the path followed by 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:
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 likev0.2
that are not on the path back from HEAD, but a path that diverged from there.git describe --tags
返回当前分支能够看到的最后一个标签
git describe --tags
returns the last tag able to be seen by current branch
获取带有前缀“ac”的最后一个标签。例如,以
ac1.0.0
或ac1.0.5
命名的标记。其他名为1.0.0
、1.1.0
的标签将被忽略。获取最后一个标签,其第一个字符是
0-9
。因此,那些第一个字符为az
的标签将被忽略。更多信息
使用
git tag --help
更新有关
sort
参数的信息。如果tag.sort
属性不存在,它将默认使用lexicorgraphic order
。经过google,有人说 git 2.8.0 支持以下语法。
Get the last tag with prefix "ac". For example, tag named with
ac1.0.0
, orac1.0.5
. Other tags named1.0.0
,1.1.0
will be ignored.Get the last tag, whose first char is
0-9
. So, those tags with first chara-z
will be ignored.More info
Update
With
git tag --help
, about thesort
argument. It will uselexicorgraphic order
by default, iftag.sort
property doesn't exist.After google, someone said git 2.8.0 support following syntax.
如果您需要多个最后一个标签
(git描述--tags有时会给出错误的哈希值,我不知道为什么,但对我来说--max-count 2不起作用)
这就是您获取列表的方式最新的 2 个标签名称按时间倒序排列,在 git 1.8.4 上完美运行。
对于早期版本的 git(如 1.7.*),输出中没有“tag:”字符串 - 只需删除最后一个 sed 调用
如果您想要超过 2 个最新标签 - 将此“sed 2q”更改为“sed 5q”或其他你需要
然后你可以轻松地将每个标签名称解析为变量等。
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)
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
If you want more than 2 latest tags - change this "sed 2q" to "sed 5q" or whatever you need
Then you can easily parse every tag name to variable or so.
所有建议有什么错误(除了Matthew Brett解释,此答案帖子的最新内容)?
当您处于不同的历史点时,只需在 jQuery Git 历史记录上运行其他人提供的任何命令,并使用视觉标记历史表示检查结果(我做了这就是您看到这篇文章的原因):
今天的许多项目在与主线不同的分支中执行发布(以及标记)。
这有强有力的理由。只要看看任何成熟的 JS/CSS 项目即可。对于用户约定,它们在 DVCS 中携带二进制/缩小版本文件。当然,作为项目维护者,您不希望用无用的二进制 blob 来破坏主线 diff 历史记录,并在主线之外执行构建工件的提交。
因为 Git 使用 DAG 而不是线性历史 - 很难定义距离度量,所以我们可以说 - 哦,那个 rev 最接近我的
HEAD
!我开始自己的旅程(看看里面,我没有将精美的校样图像复制到这篇长文章中):
过去相对于 Git 中的分支来说最近的标签是什么?
目前我对标签和修订之间的距离有 4 个合理的定义,随着距离的减小有用性:
HEAD
到合并基地的最短路径长度,带有HEAD
和标签我不知道如何计算最短路径的长度。
根据
HEAD
和标签之间的合并基础的日期对标签进行排序的脚本:它可用于大多数项目。
根据可从 HEAD 访问但无法从标签访问的转数对标签进行排序的脚本:
如果您的项目历史记录在提交时有奇怪的日期(由于变基或其他历史记录重写或某些白痴忘记更换 BIOS)电池或您在历史上所做的其他魔法)使用上面的脚本。
对于最后一个选项(标签的日期,无论合并基础)获取按日期排序的标签列表,请使用:
要获取当前修订日期,请使用:
请注意
git 描述--tags
有其自身的用途,但不能用于查找项目历史记录中人类预期的最近标签。注意您可以在任何修订中使用上述配方,只需将
HEAD
替换为您想要的即可!What is wrong with all suggestions (except Matthew Brett explanation, up to date of this answer post)?
Just run any command supplied by other on jQuery Git history when you at different point of history and check result with visual tagging history representation (I did that is why you see this post):
Todays many project perform releases (and so tagging) in separate branch from mainline.
There are strong reason for 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 diff history with useless binary blobs and perform commit of build artifacts out of mainline.
Because Git uses DAG and not linear history - it is hard to define distance metric so we can say - oh that rev is most nearest to my
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:
HEAD
to merge base with tagHEAD
and tagI don't know how to calculate length of shortest path.
Script that sort tags according to date of merge base between
HEAD
and tag:It usable on most of projects.
Script that sort tags according to number of revs that reachable from HEAD but not reachable from tag:
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.
For last option (date of tag regardless merge base) to get list of tags sorted by date use:
To get known current revision date use:
Note that
git describe --tags
have usage on its own cases but not for finding human expected nearest tag in project history.NOTE You can use above recipes on any revision, just replace
HEAD
with what you want!如果您需要最后两个标签(例如,为了生成当前标签和前一个标签之间的更改日志),以下内容对我有用。我仅在最新标签为
HEAD
的情况下对其进行了测试。它适合我的需求,但由于我不是 git 向导,我确信它可以进一步改进。我还怀疑如果提交历史向前推进,它会中断。我只是分享以防它对某人有帮助。
The following works for me in case you need last two tags (for example, in order to generate change log between current tag and the previous tag). I've tested it only in situation where the latest tag was the
HEAD
.It suits my needs, but as I'm no git wizard, I'm sure it could be further improved. I also suspect it will break in case the commit history moves forward. I'm just sharing in case it helps someone.
如果您想查找应用于特定分支的最后一个标签,您可以尝试以下操作:
If you want to find the last tag that was applied on a specific branch you can try the following:
如果您需要一个单行程序来获取当前分支上的最新标签名称(按标签日期):
我们使用它来设置设置中的版本号。
输出示例:
也适用于 Windows。
If you need a one liner which gets the latest tag name (by tag date) on the current branch:
We use this to set the version number in the setup.
Output example:
Works on Windows, too.
我的第一个想法是你可以使用 git rev-list HEAD ,它按时间倒序列出所有转速,并与 git tag --contains 结合使用。当您找到 git tag --contains 生成非空列表的引用时,您就找到了最新的标签。
My first thought is you could use
git rev-list HEAD
, which lists all the revs in reverse chronological order, in combination withgit tag --contains
. When you find a ref wheregit tag --contains
produces a nonempty list, you have found the most recent tag(s).该标签获取所有与语义版本控制匹配的分支的最新标签。
This one gets the latest tag across all branches that matches Semantic Versioning.
这是一个旧线程,但似乎很多人都错过了OP问题的最简单、最简单、最正确的答案:要获取当前分支的最新标签,您可以使用
git 描述 HEAD
。完毕。编辑:您还可以提供任何有效的引用名称,甚至是遥控器;即,
gitdescribeorigin/master
会告诉您可以从origin/master到达的最新标签。This is an old thread, but it seems a lot of people are missing the simplest, easiest, and most correct answer to OP's question: to get the latest tag for the current branch, you use
git describe HEAD
. Done.Edit: you can also supply any valid refname, even remotes; i.e.,
git describe origin/master
will tell you the latest tag that can be reached from origin/master.对于所问的问题,
您想要的
最新标签名称
--first-parent
告诉git log
不要详细说明任何合并的历史记录,--pretty=% d
表示仅显示装饰,即任何提交的本地名称。grep -m1
表示“仅匹配一个”,因此您只获得最新的标签。For the question as asked,
you want
--first-parent
tellsgit log
not to detail any merged histories,--pretty=%d
says to show only the decorations i.e. local names for any commits.grep -m1
says "match just one", so you get just the most-recent tag.要仅获取以当前分支为前缀的当前分支/标签名称的最新标签,我必须执行以下命令
我最后需要增加并为下一个标记获取标签+1。
To get the latest tag only on the current branch/tag name that prefixes with current branch, I had to execute the following
And my final need to increment and get the tag +1 for next tagging.
如果您的标签是可排序的:
if your tags are sortable:
这里没有太多提及未注释的标签与带注释的标签。 'describe' 适用于带注释的标签并忽略未注释的标签。
这很丑陋,但确实完成了请求的作业,并且不会在其他分支上找到任何标签(而不是在命令中指定的标签上:下例中的 master )
过滤应该优化(合并),但同样,这似乎到工作。
欢迎批评,因为我现在要使用它:)
Not much mention of unannotated tags vs annotated ones here. 'describe' works on annotated tags and ignores unannotated ones.
This is ugly but does the job requested and it will not find any tags on other branches (and not on the one specified in the command: master in the example below)
The filtering should prob be optimized (consolidated), but again, this seems to the the job.
Critiques welcome as I am going now to put this to use :)
我正在使用这个 bash oneline 命令来获取最新标签或分支名称:
这可以帮助我设置应用程序的版本。
I'm using this bash oneline command to get either the latest tag, either the branch name :
This helps me to set the version of my app.
下面的命令似乎可以实现从当前提交创建的最近标记(对 glob 模式还不太满意):
Below is the command that seems to work to achieve the recently tag created from the current commit (not comfortable yet with the glob pattern):