显示您所在的 git 标签?
我无法找出当前签出的标签。
当我这样做时:
git checkout tag1
git branch
我似乎无法找出我所在的标签。它只记录:
* (no branch)
master
是否可以找出哪些标签被签出?在上面的示例中,这将是 tag1
。
I'm having trouble finding out which tag is currently checked out.
When I do:
git checkout tag1
git branch
I can't seem to find out which tag I'm on. It only logs:
* (no branch)
master
Is it possible to find out which tags are checked out? In the above example, this would be tag1
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Jakub Narębski 有更多 git-fu。以下更简单的命令可以完美运行:
或者如果您已签出带注释的标签,则无需使用
--tags
。我的标签是轻量级的,所以我需要--tags
。Jakub Narębski has more git-fu. The following much simpler command works perfectly:
Or without the
--tags
if you have checked out an annotated tag. My tag is lightweight, so I need the--tags
.这对我有用
git describe --tags --abbrev=0
编辑 2020:正如下面的一些评论所提到的,这可能对你有用,也可能对你不起作用,所以要小心!
This worked for me
git describe --tags --abbrev=0
Edit 2020: As mentioned by some of the comments below, this might, or might not work for you, so be careful!
显示当前 HEAD(或提交)上的所有标签
Show all tags on current HEAD (or commit)
git describe
是一个 porcelain 命令,您应该避免使用:http://git-blame.blogspot.com/2013/06/checking-current-branch-programatically.html
相反,我使用了:
git describe
is a porcelain command, which you should avoid:http://git-blame.blogspot.com/2013/06/checking-current-branch-programatically.html
Instead, I used:
当您签出标签时,您会看到所谓的“分离头”。通常,Git 的 HEAD 提交是指向您当前已签出的分支的指针。但是,如果您检查本地分支(例如标签或远程分支)以外的其他内容,那么您就有一个“分离的头”——您实际上并不在任何分支上。当你处于独立状态时,你不应该做出任何提交。
如果您不想进行任何编辑,可以查看标签。如果您只是检查文件的内容,或者您想从标签构建项目,则可以
git checkout my_tag
并使用这些文件,只要您不这样做即可。不要进行任何提交。如果你想开始修改文件,你应该根据标签创建一个分支:将从
my_tag
开始创建一个名为my_tag_branch
的新分支。在此分支上提交更改是安全的。When you check out a tag, you have what's called a "detached head". Normally, Git's HEAD commit is a pointer to the branch that you currently have checked out. However, if you check out something other than a local branch (a tag or a remote branch, for example) you have a "detached head" -- you're not really on any branch. You should not make any commits while on a detached head.
It's okay to check out a tag if you don't want to make any edits. If you're just examining the contents of files, or you want to build your project from a tag, it's okay to
git checkout my_tag
and work with the files, as long as you don't make any commits. If you want to start modifying files, you should create a branch based on the tag:will create a new branch called
my_tag_branch
starting frommy_tag
. It's safe to commit changes on this branch.git log --decorate
这将告诉您哪些引用指向当前签出的提交。
git log --decorate
This will tell you what refs are pointing to the currently checked out commit.
这是针对一组特定用例的有趣示例。如果您的存储库具有
v1.0.0
、v1.1.0
、v1.1.1
等版本,以及简写版本,例如v1
指向最新的v1.xx
,以下内容将为您提供与最新完整版本标记相关的当前签出提交的引用,并带有回退如果这不起作用:那么假设您有以下提交:
上面命令的几个示例 HEAD 的输出:
git checkout main
->v2.0.0
git checkout 3333333
->v1.1.0-1-g3333333
git checkout 2222222
->v1.1.0
git checkout v1
->v1.1.0
git checkout 0000000
->0000000
(完整参考)我发现这对于 GitHub Actions 存储库很有用,您需要在其中维护一组详细的速记版本标签。
Here's a fun one for a certain set of use cases. If your repository has versions such as
v1.0.0
,v1.1.0
,v1.1.1
etc, and also shorthand versions such asv1
that point to whatever is the latestv1.x.x
, the following will give you a reference to the currently-checked-out commit in relation to the most recent fully versioned tag, with fallbacks if that doesn't work:So let's say you have the following commits:
Output of the above command for a few example HEADs:
git checkout main
->v2.0.0
git checkout 3333333
->v1.1.0-1-g3333333
git checkout 2222222
->v1.1.0
git checkout v1
->v1.1.0
git checkout 0000000
->0000000
(full ref)I've found this useful for GitHub Actions repositories, where you're expected to maintain a verbose set of shorthand version tags.