簡體   English   中英

如何找到與給定 git commit 關聯的標簽?

[英]How to find the tag associated with a given git commit?

對於版本,我通常使用 v1.1.0 之類的標記。 在我的構建腳本中,我創建了一個包含當前 git 信息的 fwVersion.c 文件。 目前,我在文件中有提交和分支信息,但我想添加標簽。

這可能嗎?

檢查git describe的文檔。 它找到與給定提交最近的標簽(即指向提交祖先的標簽)並根據標簽描述該提交。

如果您只想知道提交是否由標記指向,那么您可以檢查以下輸出:

git describe --exact-match <commit-id>

如果你想要的是包含提交的第一個標簽,那么:

git describe --contains <commit>

給出了最好的答案 IMO。 如果您有頻繁的標簽而不是在大型存儲庫中的舊提交上使用“git tag --contains”可能需要一段時間才能運行並為您提供包含該提交的所有標簽。

這種形式的 git describe 運行得非常快,並為您提供一個輸出值,它是包含提交的第一個標簽以及您提交的回溯時間。

這個怎么樣?

git tag --points-at <commit-id>

它為您提供給定提交具有的所有標簽(而git describe只給您一個),並且不包括后代提交上的標簽(如git tag --contains所做的那樣)。

您可以在手冊中找到此信息

git tag --contains <commit>

我找到了兩個最高答案的組合,給了我我想要的東西:

git describe --tags --exact-match <commit-id>

這為您提供了僅用於該提交和沒有注釋的標簽。 當您想查找標簽而不用擔心剝離格式時很有用(例如對於 Jenkins)。

例如。 $ git describe --tags --exact-match head~2

給你:

$ ReleaseBeta

綜合一些答案:

git tag --contains [<ref>]

git tag --points-at [<ref>]

要不就

git tag

行為相同,如果未指定,則為指定的引用或當前提交打印任何(和所有)標簽。

git describe --tags [<ref>]

其中<ref>默認為當前提交,如果沒有標簽與提交關聯,則以 128 退出,並打印與提交關聯的標簽(似乎沒有模式)。

git describe [<ref>]行為與--tags相同,只是它只打印帶注釋的標簽。

提供選項--containsdescribe將打印與指定提交的祖先相關聯的標簽。 例如

$ git init
Initialized empty Git repository in /tmp/test
$ git commit -m one --allow-empty
[master (root-commit) 7fdfff2] one
$ git commit -m two --allow-empty
[master cd5f8f1] two
$ git tag -am foo foo
$ git tag bar
$ git log --format=oneline
cd5f8f1f4f29eb164f83e224768ccaf37fe170ed (HEAD -> master, tag: foo, tag: bar) two
7fdfff2ce5e3347f8eee4c9f2413dbd4e90060e1 one
$ git describe 7fdfff2ce5e3347f8eee4c9f2413dbd4e90060e1
fatal: No tags can describe '7fdfff2ce5e3347f8eee4c9f2413dbd4e90060e1'.
Try --always, or create some tags.
$ git describe --contains 7fdfff2ce5e3347f8eee4c9f2413dbd4e90060e1
bar~1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM