显示您所在的 git 标签?

发布于 2024-09-12 15:00:51 字数 219 浏览 3 评论 0原文

我无法找出当前签出的标签。

当我这样做时:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

内心激荡 2024-09-19 15:00:51

Jakub Narębski 有更多 git-fu。以下更简单的命令可以完美运行:

git describe --exact-match --tags

或者如果您已签出带注释的标签,则无需使用 --tags 。我的标签是轻量级的,所以我需要 --tags

Jakub Narębski has more git-fu. The following much simpler command works perfectly:

git describe --exact-match --tags

Or without the --tags if you have checked out an annotated tag. My tag is lightweight, so I need the --tags.

拒绝两难 2024-09-19 15:00:51

这对我有用 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!

傲鸠 2024-09-19 15:00:51

显示当前 HEAD(或提交)上的所有标签

git tag --points-at HEAD

Show all tags on current HEAD (or commit)

git tag --points-at HEAD
圈圈圆圆圈圈 2024-09-19 15:00:51

git describe 是一个 porcelain 命令,您应该避免使用:

http://git-blame.blogspot.com/2013/06/checking-current-branch-programatically.html

相反,我使用了:

git name-rev --tags --name-only $(git rev-parse HEAD)

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 name-rev --tags --name-only $(git rev-parse HEAD)
吝吻 2024-09-19 15:00:51

当您签出标签时,您会看到所谓的“分离头”。通常,Git 的 HEAD 提交是指向您当前已签出的分支的指针。但是,如果您检查本地分支(例如标签或远程分支)以外的其他内容,那么您就有一个“分离的头”——您实际上并不在任何分支上。当你处于独立状态时,你不应该做出任何提交。

如果您不想进行任何编辑,可以查看标签。如果您只是检查文件的内容,或者您​​想从标签构建项目,则可以 git checkout my_tag 并使用这些文件,只要您不这样做即可。不要进行任何提交。如果你想开始修改文件,你应该根据标签创建一个分支:

$ git checkout -b my_tag_branch 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:

$ git checkout -b my_tag_branch my_tag

will create a new branch called my_tag_branch starting from my_tag. It's safe to commit changes on this branch.

倾听心声的旋律 2024-09-19 15:00:51

git log --decorate

这将告诉您哪些引用指向当前签出的提交。

git log --decorate

This will tell you what refs are pointing to the currently checked out commit.

九公里浅绿 2024-09-19 15:00:51

这是针对一组特定用例的有趣示例。如果您的存储库具有 v1.0.0v1.1.0v1.1.1 等版本,以及简写版本,例如 v1 指向最新的 v1.xx,以下内容将为您提供与最新完整版本标记相关的当前签出提交的引用,并带有回退如果这不起作用:

git describe --tags --exact-match --match "v*.*.*" \
  || git describe --match "v*.*.*" --tags \
  || git describe --tags \
  || git rev-parse HEAD

那么假设您有以下提交:

* 4444444 (main, origin/main, tag: v2.0.0, tag: v2.0, tag: v2)
* 3333333
* 2222222 (tag: v1.1.0, tag: v1.1, tag: v1)
* 1111111 (tag: v1.0.0, tag: v1.0)
* 0000000

上面命令的几个示例 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 as v1 that point to whatever is the latest v1.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:

git describe --tags --exact-match --match "v*.*.*" \
  || git describe --match "v*.*.*" --tags \
  || git describe --tags \
  || git rev-parse HEAD

So let's say you have the following commits:

* 4444444 (main, origin/main, tag: v2.0.0, tag: v2.0, tag: v2)
* 3333333
* 2222222 (tag: v1.1.0, tag: v1.1, tag: v1)
* 1111111 (tag: v1.0.0, tag: v1.0)
* 0000000

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文