Git:列出悬挂标签

7

背景:

  • 假设您有一些相当棘手的CI/CD工作流,它依赖于git标签
  • 功能分支被构建并生成一些短期的标签,表示产生可部署构件的提交
  • 当功能分支被压缩合并时,通常会被删除,但是标签毫不意外地幸存了下来
  • 经过几个月的开发后,标签列表可预见地变得混乱

因此,问题是:

我该如何使用git命令行和基本的bash工具:

  1. 列出所有具有特定标签的分支(对应的是git tag -l --merged ${BRANCH_COMMITTISH},但我不需要给定分支的标签,而是需要给定标签的分支)
  2. 列出从第1点中输出为空的所有标签(显然可以通过for循环(在1的任何终止实现的情况下)完成,但也许有一些神奇的git单行代码)?
3个回答

6
git log --simplify-by-decoration --tags --not --branches --remotes --pretty=%d

--simplify-by-decoration表示仅显示揭示祖先关系所需的最少信息(通常与--graph一起使用)。 --tags --not --branches --remotes的意思是列出未在分支或远程历史记录中的标签历史记录,即无法从任何分支或远程跟踪分支到达的标签。 --pretty=%d表示仅显示引用。


非常感谢!这是我自己从未发现或解决的问题。非常感谢! - Anton Kraievyi

3

哦,好吧,我必须再多读一会儿该死的手册,谢谢! - Anton Kraievyi

0

仅为了说明目前我看到的所有解决方案:

~$ git init dangling_tags
Initialized empty Git repository in ~/dangling_tags/.git/
$ cd dangling_tags/
~/dangling_tags$ touch a.txt && git add a.txt && git commit -m a
[master (root-commit) f1b1070] a
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a.txt
~/dangling_tags$ git tag a
~/dangling_tags$ git checkout -b feature/add_some_tags
Switched to a new branch 'feature/add_some_tags'
~/dangling_tags$ touch b.txt && git add b.txt && git commit -m b 
[feature/add_some_tags 1871cde] b
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 b.txt
~/dangling_tags$ git tag b
~/dangling_tags$ touch c.txt && git add c.txt && git commit -m c 
[feature/add_some_tags 26f6611] c
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 c.txt
~/dangling_tags$ git tag c
~/dangling_tags$ git checkout master
Switched to branch 'master'
~/dangling_tags$ git merge --squash feature/add_some_tags 
Updating f1b1070..26f6611
Fast-forward
Squash commit -- not updating HEAD
 b.txt | 0
 c.txt | 0
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 b.txt
 create mode 100644 c.txt
~/dangling_tags$ git commit
[master 99b33ae] Squashed commit of the following:
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 b.txt
 create mode 100644 c.txt
~/dangling_tags$ git branch --contains a
* master
~/dangling_tags$ git branch --contains b
  feature/add_some_tags 
~/dangling_tags$ git branch -D feature/add_some_tags 
Deleted branch feature/add_some_tags (was 26f6611).
~/dangling_tags$ git tag
a
b
c
~/dangling_tags$ git branch --contains a
* master
~/dangling_tags$ git branch --contains b
~/dangling_tags$ for t in $(git tag); do if test "$(git branch --contains $t | wc -l)" == "0" ; then echo $t; fi done
b
c
~/dangling_tags$ git log --simplify-by-decoration --tags --not --branches --remotes --pretty=%d
 (tag: c)
 (tag: b)

在倒数第二个命令中 for t in $(git tag); do if test "$(git branch --contains $t | wc -l)" == "0" ; then echo $t; fi done,缺少一个分号,应该在 done 前加上分号。但我无法编辑答案以添加它,因为只有 6 个字符的更改才是有效的编辑。 - muzimuzhi Z

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接