显示git日志中的所有标签

mnowg1ta  于 2022-12-17  发布在  Git
关注(0)|答案(4)|浏览(138)

为什么git log --decorate每次提交不显示多个标记?
编辑:查尔斯·贝利已经想出了答案(至少在我的情况下)

本质上,我有一个标记指向另一个指向提交的标记。因为这个额外的间接层,标记没有显示在日志中。我必须修复这个问题,通过修复我们的标记脚本来正确标记,或者通过一些shell脚本voodoo递归地跟随标记。无论如何,我将把这个问题放在这里,以供大家参考(我是第一次接触堆栈溢出,但我认为这是正确的协议?)

...原始问题如下...

背景:我们在工作中使用GIT进行源代码控制,并且我们有一个策略,即在部署时总是标记提交。(实际上,它是一个脚本,可以做标记,然后将标记拉到服务器上)由于它是一个Web应用程序,具有单独的登台服务器和生产服务器,因此我们经常标记一个发布用于登台(用于测试或其他),然后标记同一个提交用于生产。
所以我们经常会在同一个提交中有多个标记。如果能在文本日志中看到这一点就好了,但它似乎不支持。我目前正在通过手动检查要查找的标记或启动gitk来解决这个问题。虽然这两种解决方案都有效,在我看来,git log --decorate在默认情况下每次提交只支持一个标记,这真的很奇怪。
我用谷歌搜索了一下,但没什么发现。我错过了什么明显的东西吗?

  • P.S.(实际上,我对%d使用了自定义格式字符串,根据手册页和一些快速测试,它等效于--decorate)*
vuktfyat

vuktfyat1#

git log --no-walk --tags --pretty="%h %d %s" --decorate=full

这个版本也会打印提交消息:

$ git log --no-walk --tags --pretty="%h %d %s" --decorate=full
3713f3f  (tag: refs/tags/1.0.0, tag: refs/tags/0.6.0, refs/remotes/origin/master, refs/heads/master) SP-144/ISP-177: Updating the package.json with 0.6.0 version and the README.md.
00a3762  (tag: refs/tags/0.5.0) ISP-144/ISP-205: Update logger to save files with optional port number if defined/passed: Version 0.5.0
d8db998  (tag: refs/tags/0.4.2) ISP-141/ISP-184/ISP-187: Fixing the bug when loading the app with Gulp and Grunt for 0.4.2
3652484  (tag: refs/tags/0.4.1) ISP-141/ISP-184: Missing the package.json and README.md updates with the 0.4.1 version
c55eee7  (tag: refs/tags/0.4.0) ISP-141/ISP-184/ISP-187: Updating the README.md file with the latest 1.3.0 version.
6963d0b  (tag: refs/tags/0.3.0) ISP-141/ISP-184: Add support for custom serializers: README update
4afdbbe  (tag: refs/tags/0.2.0) ISP-141/ISP-143/ISP-144: Fixing a bug with the creation of the logs
e1513f1  (tag: refs/tags/0.1.0) ISP-141/ISP-143: Betterr refactoring of the Loggers, no dependencies, self-configuration for missing settings.
daolsyd0

daolsyd02#

请注意tag of tag(标记一个标记),这是问题的根源,正如Charles Bailey在评论中正确指出的那样:
一定要学习this thread,因为覆盖一个带符号的标记并不容易:

  • 如果您已经推送了一个标记,git tag手册页会严肃地建议不要用简单的git tag -f B来替换标记名“A
  • 不要尝试用git tag -f重新创建一个带符号的标记(参见下面的线程摘录)

(it是关于一个极端案例,但对一般的标签很有启发性,它来自另一个SO贡献者Jakub Narębski):
请注意,标签(重量级标签,即标签对象)的名称存储在两个位置:

  • 在标记对象本身中作为“标记”报头的内容(您可以在“git show <tag>“的输出中以及在“git cat-file -p <tag>“的输出中看到它,其中<tag>是重量级标记,例如git.git储存库中的v1.6.3),
  • 并且也是指向标签对象的标签引用(“refs/tags/*“命名空间中的引用)的默认名称。

注意,标签 reference(“refs/tags/*“命名空间中的适当引用)纯粹是***本地***的事情;例如,一个储存库在“refs/tags/v0.1.3”中具有什么,其它储存库可以在“refs/tags/sub/v0.1.3”中具有什么。
因此,当您创建带符号标记'A'时,您会遇到以下情况(假设它指向某个提交)

35805ce   <--- 5b7b4ead  <=== refs/tags/A
  (commit)       tag A
                 (tag)

还请注意,“git tag -f A A“(注意没有强制它成为带注解标记的选项)是一个noop -它不会改变情况。
如果执行“git tag -f -s A A“:注意,如果你 force owerwriting一个标签(所以git假设你知道你在做什么),并且-s/-a/-m选项之一被用来强制注解标签(创建标签对象),你会得到以下情况

35805ce   <--- 5b7b4ea  <--- ada8ddc  <=== refs/tags/A
  (commit)       tag A         tag A
                 (tag)         (tag)

还要注意,“git show A“将显示向下到非标记对象的整个链...

wvmv3b1j

wvmv3b1j3#

注意:brian m. carlson ( bk2204 )(适用于git 1.9/2.0 Q1 2014)中的commit 5e1361c处理了一个特殊情况,即用标签装饰日志:
日志:正确处理带有链接标签的装饰

git log在标记对象引用不再是ref的另一个标记对象时(例如删除第二个标记时)无法正确处理装饰

由于parse_object没有在第二个标签上被调用,因此其标签字段没有被填充,从而导致没有标签与相关提交相关联,因此提交将不会被正确修饰。
调用parse_object来填充这个字段,如果它不存在的话,这样标签链就可以被取消引用,并且提交可以被正确地修饰。
包括测试,以及防止未来的回归。
示例:

git tag -a tag1 -m tag1 &&
git tag -a tag2 -m tag2 tag1 &&
git tag -d tag1 &&
git commit --amend -m shorter &&
git log --no-walk --tags --pretty="%H %d" --decorate=full
ovfsdjhp

ovfsdjhp4#

注:我只是针对问题的 * 主题 *(着重部分由作者标明):

显示git log中的所有标记

...而不是问题的具体细节。

显示每个标记提交的完整git log(彩色!):

我真的认为这是显示所有标签的最有用和最漂亮的表单:

# standard, multi-line `git log` output
git log --no-walk --tags

# concise, one-line `git log` output
git log --no-walk --tags --oneline

它显示了每个附加了标签的提交的完整git log输出。非常方便!
为了理解man git log中的选项描述,我认为--tags会导致git log显示所有标记,即使标记不在git log祖先树中的其他提交上,而--no-walk会告诉它只显示标记,而不是遍历树并显示所有未标记的提交。添加是为了获得每个提交一行的简洁输出(在本例中实际上是标记)。
如果你想过滤掉与某个字符串搜索模式匹配的标签,可以这样做:

# multi-line output
git log --no-walk --tags=*some_string*

# one-line output
git log --no-walk --tags=*some_string* --oneline

示例:

git log --no-walk --tags=*release/my_board*

每个标记提交的可选单行git log摘要(彩色!):

如果你想在你的one-line-per-tagged-commit输出中看到更多的信息,可以这样做。这是从Coderwall's beautiful git lg alias修改而来的:

# fancy one-line output
git log --color --no-walk --tags --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

# vs regular one-line output
git log --no-walk --tags --oneline

下面是official "Stable" version of the Linux kernel的一些示例输出:
1.“fancy”单行命令和输出

linux_stable$ git log --color --no-walk --tags --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
650093916eb3 - (tag: v6.0.12, origin/linux-6.0.y) Linux 6.0.12 (5 days ago) <Greg Kroah-Hartman>
d9790301361c - (tag: v5.15.82, origin/linux-5.15.y) Linux 5.15.82 (5 days ago) <Greg Kroah-Hartman>
592346d5dc9b - (tag: v5.10.158, origin/linux-5.10.y) Linux 5.10.158 (5 days ago) <Greg Kroah-Hartman>
316cdfc48d4d - (tag: v5.4.226, origin/linux-5.4.y) Linux 5.4.226 (5 days ago) <Greg Kroah-Hartman>
e8fff2341723 - (tag: v4.19.268, origin/linux-4.19.y) Linux 4.19.268 (5 days ago) <Greg Kroah-Hartman>
65afe34ac33d - (tag: v4.14.301, origin/linux-4.14.y) Linux 4.14.301 (5 days ago) <Greg Kroah-Hartman>
b6097015eea4 - (tag: v4.9.335, origin/linux-4.9.y) Linux 4.9.335 (5 days ago) <Greg Kroah-Hartman>
76dcd734eca2 - (tag: v6.1-rc8) Linux 6.1-rc8 (8 days ago) <Linus Torvalds>
31e4bdd2c25b - (tag: v6.0.11) Linux 6.0.11 (10 days ago) <Greg Kroah-Hartman>
e4a7232c917c - (tag: v5.15.81) Linux 5.15.81 (10 days ago) <Greg Kroah-Hartman>

1.常规的单行命令,并在完全相同的时间点输出

linux_stable$ git log --no-walk --tags --oneline
650093916eb3 (tag: v6.0.12, origin/linux-6.0.y) Linux 6.0.12
d9790301361c (tag: v5.15.82, origin/linux-5.15.y) Linux 5.15.82
592346d5dc9b (tag: v5.10.158, origin/linux-5.10.y) Linux 5.10.158
316cdfc48d4d (tag: v5.4.226, origin/linux-5.4.y) Linux 5.4.226
e8fff2341723 (tag: v4.19.268, origin/linux-4.19.y) Linux 4.19.268
65afe34ac33d (tag: v4.14.301, origin/linux-4.14.y) Linux 4.14.301
b6097015eea4 (tag: v4.9.335, origin/linux-4.9.y) Linux 4.9.335
76dcd734eca2 (tag: v6.1-rc8) Linux 6.1-rc8
31e4bdd2c25b (tag: v6.0.11) Linux 6.0.11
e4a7232c917c (tag: v5.15.81) Linux 5.15.81

当然,您仍然可以使用--tags=*some_string*表单过滤您想要的特定标签:

git log --color --no-walk --tags=*some_string* --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

示例:

git log --color --no-walk --tags=*release/my_board* --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

参考文献:

  1. answer by @Marcello de Sales帮助我开始。
  2. Coderwall's beautiful git lg alias
  3. man git log,以及我个人的实验

相关问题