显示您在哪个 git 标签上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3404936/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:51:30  来源:igfitidea点击:

Show which git tag you are on?

gitgit-checkoutgit-tag

提问by grm

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.

是否可以找出哪些标签被检出?在上面的例子中,这将是tag1.

回答by bstpierre

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

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

git describe --tags

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

(或者--tags如果您签出带注释的标签,则没有。我的标签是轻量级的,所以我需要 --tags。)

original answer follows:

原答案如下:

git describe --exact-match --tags $(git log -n1 --pretty='%h')

Someone with more git-fu may have a more elegant solution...

有更多 git-fu 的人可能有更优雅的解决方案......

This leverages the fact that git-logreports the log starting from what you've checked out. %hprints the abbreviated hash. Then git describe --exact-match --tagsfinds the tag (lightweight or annotated) that exactly matches that commit.

这利用了git-log从您签出的内容开始报告日志的事实。%h打印缩写的哈希。然后git describe --exact-match --tags找到与该提交完全匹配的标签(轻量级或带注释)。

The $()syntax above assumes you're using bash or similar.

$()上面的语法假设您使用的是 bash 或类似的。

回答by M K

This worked for me git describe --tags --abbrev=0

这对我有用 git describe --tags --abbrev=0

回答by George Pavelka

Show all tags on current HEAD (or commit)

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

git tag --points-at HEAD

回答by Greg

git describeis a porcelaincommand, which you should avoid:

git describe是一个瓷器命令,你应该避免:

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

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)

回答by mipadi

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.

当您签出标签时,您会看到所谓的“分离头”。通常,Git 的 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_tagand 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 my_tag那么只要您不进行任何提交,就可以使用这些文件。如果你想开始修改文件,你应该根据标签创建一个分支:

$ git checkout -b my_tag_branch my_tag

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

将创建一个名为my_tag_branch开始的新分支my_tag。在此分支上提交更改是安全的。

回答by chriswatrous

git log --decorate

git log --decorate

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

这将告诉您哪些 refs 指向当前已检出的提交。